Skip to content

Commit 0744844

Browse files
committed
Factor out process_one_module into a function (NFC)
1 parent 89df8d4 commit 0744844

File tree

1 file changed

+139
-137
lines changed

1 file changed

+139
-137
lines changed

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

Lines changed: 139 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,6 +1941,140 @@ static lldb::ModuleSP GetUnitTestModule(lldb_private::ModuleList &modules) {
19411941
return ModuleSP();
19421942
}
19431943

1944+
/// Scan a newly added lldb::Module fdor Swift modules and report any errors in
1945+
/// its module SwiftASTContext to Target.
1946+
static void
1947+
ProcessModule(ModuleSP &&module_sp, std::string m_description,
1948+
bool use_all_compiler_flags, Target &target,
1949+
std::vector<std::string> &module_search_paths,
1950+
std::vector<std::pair<std::string, bool>> &framework_search_paths,
1951+
std::vector<std::string> &extra_clang_args) {
1952+
const FileSpec &module_file = module_sp->GetFileSpec();
1953+
std::string module_path = module_file.GetPath();
1954+
1955+
// Add the containing framework to the framework search path.
1956+
// Don't do that if this is the executable module, since it
1957+
// might be buried in some framework that we don't care about.
1958+
if (use_all_compiler_flags &&
1959+
target.GetExecutableModulePointer() != module_sp.get()) {
1960+
size_t framework_offset = module_path.rfind(".framework/");
1961+
1962+
if (framework_offset != std::string::npos) {
1963+
// Sometimes the version of the framework that got loaded has been
1964+
// stripped and in that case, adding it to the framework search
1965+
// path will just short-cut a clang search that might otherwise
1966+
// find the needed headers. So don't add these paths.
1967+
std::string framework_path = module_path.substr(0, framework_offset);
1968+
framework_path.append(".framework");
1969+
FileSpec path_spec(framework_path);
1970+
FileSystem::Instance().Resolve(path_spec);
1971+
FileSpec headers_spec = path_spec.CopyByAppendingPathComponent("Headers");
1972+
bool add_it = false;
1973+
if (FileSystem::Instance().Exists(headers_spec))
1974+
add_it = true;
1975+
if (!add_it) {
1976+
FileSpec module_spec =
1977+
path_spec.CopyByAppendingPathComponent("Modules");
1978+
if (FileSystem::Instance().Exists(module_spec))
1979+
add_it = true;
1980+
}
1981+
1982+
if (!add_it) {
1983+
LOG_PRINTF(LIBLLDB_LOG_TYPES,
1984+
"ProcessModule(\"%s\") rejecting framework path \"%s\" "
1985+
"as it has no \"Headers\" or \"Modules\" subdirectories.",
1986+
module_file.GetFilename().AsCString(""),
1987+
framework_path.c_str());
1988+
}
1989+
1990+
if (add_it) {
1991+
while (framework_offset && (module_path[framework_offset] != '/'))
1992+
framework_offset--;
1993+
1994+
if (module_path[framework_offset] == '/') {
1995+
// framework_offset now points to the '/';
1996+
1997+
std::string parent_path = module_path.substr(0, framework_offset);
1998+
1999+
// Never add framework paths pointing into the
2000+
// system. These modules must be imported from the
2001+
// SDK instead.
2002+
if (!StringRef(parent_path).startswith("/System/Library") &&
2003+
!IsDeviceSupport(parent_path.c_str())) {
2004+
LOG_PRINTF(LIBLLDB_LOG_TYPES,
2005+
"ProcessModule(\"%s\") adding framework path \"%s\".",
2006+
module_file.GetFilename().AsCString(""),
2007+
framework_path.c_str());
2008+
framework_search_paths.push_back(
2009+
{std::move(parent_path), /*system*/ false});
2010+
}
2011+
}
2012+
}
2013+
}
2014+
}
2015+
2016+
// Skip images without a serialized Swift AST.
2017+
if (!HasSwiftModules(*module_sp))
2018+
return;
2019+
2020+
auto type_system_or_err =
2021+
module_sp->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
2022+
if (!type_system_or_err) {
2023+
llvm::consumeError(type_system_or_err.takeError());
2024+
return;
2025+
}
2026+
2027+
SwiftASTContext *ast_context =
2028+
llvm::dyn_cast_or_null<SwiftASTContext>(&*type_system_or_err);
2029+
2030+
if (!ast_context || ast_context->HasFatalErrors() ||
2031+
!ast_context->GetClangImporter()) {
2032+
// Make sure we warn about this module load failure, the one
2033+
// that comes from loading types often gets swallowed up and not
2034+
// seen, this is the only reliable point where we can show this.
2035+
// But only do it once per UUID so we don't overwhelm the user
2036+
// with warnings.
2037+
UUID module_uuid(module_sp->GetUUID());
2038+
bool unique_message =
2039+
target.RegisterSwiftContextMessageKey(module_uuid.GetAsString());
2040+
if (!unique_message)
2041+
return;
2042+
std::string buf;
2043+
{
2044+
llvm::raw_string_ostream ss(buf);
2045+
module_sp->GetDescription(ss, eDescriptionLevelBrief);
2046+
if (ast_context && ast_context->HasFatalErrors())
2047+
ss << ": " << ast_context->GetFatalErrors().AsCString("unknown error");
2048+
}
2049+
target.GetDebugger().GetErrorStreamSP()->Printf(
2050+
"Error while loading Swift module:\n%s\n"
2051+
"Debug info from this module will be unavailable in the "
2052+
"debugger.\n\n",
2053+
buf.c_str());
2054+
return;
2055+
}
2056+
2057+
if (ast_context->HasErrors())
2058+
return;
2059+
if (use_all_compiler_flags ||
2060+
target.GetExecutableModulePointer() == module_sp.get()) {
2061+
2062+
const auto &opts = ast_context->GetSearchPathOptions();
2063+
module_search_paths.insert(module_search_paths.end(),
2064+
opts.ImportSearchPaths.begin(),
2065+
opts.ImportSearchPaths.end());
2066+
for (const auto &fwsp : opts.FrameworkSearchPaths)
2067+
framework_search_paths.push_back({fwsp.Path, fwsp.IsSystem});
2068+
for (const std::string &arg : ast_context->GetClangArguments()) {
2069+
extra_clang_args.push_back(arg);
2070+
if (lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TARGET))
2071+
LOG_PRINTF(LIBLLDB_LOG_TYPES,
2072+
"ProcessModule(\"%s\") adding Clang argument \"%s\".",
2073+
module_file.GetFilename().AsCString(""), arg.c_str());
2074+
}
2075+
}
2076+
}
2077+
19442078
lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
19452079
Target &target,
19462080
const char *extra_options) {
@@ -2130,144 +2264,12 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
21302264
const bool use_all_compiler_flags =
21312265
!got_serialized_options || target.GetUseAllCompilerFlags();
21322266

2133-
std::function<void(ModuleSP &&)> process_one_module =
2134-
[&](ModuleSP &&module_sp) {
2135-
const FileSpec &module_file = module_sp->GetFileSpec();
2136-
2137-
std::string module_path = module_file.GetPath();
2138-
2139-
// Add the containing framework to the framework search path.
2140-
// Don't do that if this is the executable module, since it
2141-
// might be buried in some framework that we don't care about.
2142-
if (use_all_compiler_flags &&
2143-
target.GetExecutableModulePointer() != module_sp.get()) {
2144-
size_t framework_offset = module_path.rfind(".framework/");
2145-
2146-
if (framework_offset != std::string::npos) {
2147-
// Sometimes the version of the framework that got loaded has been
2148-
// stripped and in that case, adding it to the framework search
2149-
// path will just short-cut a clang search that might otherwise
2150-
// find the needed headers. So don't add these paths.
2151-
std::string framework_path =
2152-
module_path.substr(0, framework_offset);
2153-
framework_path.append(".framework");
2154-
FileSpec path_spec(framework_path);
2155-
FileSystem::Instance().Resolve(path_spec);
2156-
FileSpec headers_spec =
2157-
path_spec.CopyByAppendingPathComponent("Headers");
2158-
bool add_it = false;
2159-
if (FileSystem::Instance().Exists(headers_spec))
2160-
add_it = true;
2161-
if (!add_it) {
2162-
FileSpec module_spec =
2163-
path_spec.CopyByAppendingPathComponent("Modules");
2164-
if (FileSystem::Instance().Exists(module_spec))
2165-
add_it = true;
2166-
}
2167-
2168-
if (!add_it) {
2169-
LOG_PRINTF(
2170-
LIBLLDB_LOG_TYPES,
2171-
"process_one_module(\"%s\") rejecting framework path \"%s\" "
2172-
"as it has no \"Headers\" or \"Modules\" subdirectories.",
2173-
module_file.GetFilename().AsCString(""),
2174-
framework_path.c_str());
2175-
}
2176-
2177-
if (add_it) {
2178-
while (framework_offset && (module_path[framework_offset] != '/'))
2179-
framework_offset--;
2180-
2181-
if (module_path[framework_offset] == '/') {
2182-
// framework_offset now points to the '/';
2183-
2184-
std::string parent_path =
2185-
module_path.substr(0, framework_offset);
2186-
2187-
// Never add framework paths pointing into the
2188-
// system. These modules must be imported from the
2189-
// SDK instead.
2190-
if (!StringRef(parent_path).startswith("/System/Library") &&
2191-
!IsDeviceSupport(parent_path.c_str())) {
2192-
LOG_PRINTF(LIBLLDB_LOG_TYPES,
2193-
"process_one_module(\"%s\") adding framework path "
2194-
"\"%s\".",
2195-
module_file.GetFilename().AsCString(""),
2196-
framework_path.c_str());
2197-
framework_search_paths.push_back(
2198-
{std::move(parent_path), /*system*/ false});
2199-
}
2200-
}
2201-
}
2202-
}
2203-
}
2204-
2205-
// Skip images without a serialized Swift AST.
2206-
if (!HasSwiftModules(*module_sp))
2207-
return;
2208-
2209-
auto type_system_or_err =
2210-
module_sp->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
2211-
if (!type_system_or_err) {
2212-
llvm::consumeError(type_system_or_err.takeError());
2213-
return;
2214-
}
2215-
2216-
SwiftASTContext *ast_context =
2217-
llvm::dyn_cast_or_null<SwiftASTContext>(&*type_system_or_err);
2218-
2219-
if (!ast_context || ast_context->HasFatalErrors() ||
2220-
!ast_context->GetClangImporter()) {
2221-
// Make sure we warn about this module load failure, the one
2222-
// that comes from loading types often gets swallowed up and not
2223-
// seen, this is the only reliable point where we can show this.
2224-
// But only do it once per UUID so we don't overwhelm the user
2225-
// with warnings.
2226-
UUID module_uuid(module_sp->GetUUID());
2227-
bool unique_message =
2228-
target.RegisterSwiftContextMessageKey(module_uuid.GetAsString());
2229-
if (!unique_message)
2230-
return;
2231-
std::string buf;
2232-
{
2233-
llvm::raw_string_ostream ss(buf);
2234-
module_sp->GetDescription(ss, eDescriptionLevelBrief);
2235-
if (ast_context && ast_context->HasFatalErrors())
2236-
ss << ": "
2237-
<< ast_context->GetFatalErrors().AsCString("unknown error");
2238-
}
2239-
target.GetDebugger().GetErrorStreamSP()->Printf(
2240-
"Error while loading Swift module:\n%s\n"
2241-
"Debug info from this module will be unavailable in the "
2242-
"debugger.\n\n",
2243-
buf.c_str());
2244-
return;
2245-
}
2246-
2247-
if (!ast_context->HasErrors()) {
2248-
if (use_all_compiler_flags ||
2249-
target.GetExecutableModulePointer() == module_sp.get()) {
2250-
2251-
const auto &opts = ast_context->GetSearchPathOptions();
2252-
module_search_paths.insert(module_search_paths.end(),
2253-
opts.ImportSearchPaths.begin(),
2254-
opts.ImportSearchPaths.end());
2255-
for (const auto &fwsp : opts.FrameworkSearchPaths)
2256-
framework_search_paths.push_back({fwsp.Path, fwsp.IsSystem});
2257-
if (lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TARGET))
2258-
for (const std::string &arg : ast_context->GetClangArguments()) {
2259-
LOG_PRINTF(
2260-
LIBLLDB_LOG_TYPES,
2261-
"process_one_module(\"%s\") adding Clang argument \"%s\".",
2262-
module_file.GetFilename().AsCString(""), arg.c_str());
2263-
}
2264-
swift_ast_sp->AddExtraClangArgs(ast_context->GetClangArguments());
2265-
}
2266-
}
2267-
};
2268-
22692267
for (size_t mi = 0; mi != num_images; ++mi) {
2270-
process_one_module(target.GetImages().GetModuleAtIndex(mi));
2268+
std::vector<std::string> extra_clang_args;
2269+
ProcessModule(target.GetImages().GetModuleAtIndex(mi), m_description,
2270+
use_all_compiler_flags, target, module_search_paths,
2271+
framework_search_paths, extra_clang_args);
2272+
swift_ast_sp->AddExtraClangArgs(extra_clang_args);
22712273
}
22722274

22732275
FileSpecList target_module_paths = target.GetSwiftModuleSearchPaths();

0 commit comments

Comments
 (0)