Skip to content

Commit 7c592dc

Browse files
committed
Move deserialization of import search paths into the scratch context
Import and framework search paths were previously not discovered by validateSerializedAST() so SwiftASTContextForExpressions had the various per-module SwiftASTContext load all registered Swift modules and then grabbed the discovered search paths from their respective deserialized CompilerInvocations. We now extended validateSerializedAST() to also deserialize the search paths, which allows getting rid of this last dependency between SwiftASTContextForExpressions and the per-module SwiftASTContexts. rdar://40097459
1 parent ece234d commit 7c592dc

File tree

1 file changed

+73
-65
lines changed

1 file changed

+73
-65
lines changed

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

Lines changed: 73 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@
5252
#include "swift/Sema/IDETypeChecking.h"
5353
#include "swift/Serialization/Validation.h"
5454
#include "swift/SymbolGraphGen/SymbolGraphOptions.h"
55+
5556
#include "clang/AST/ASTContext.h"
5657
#include "clang/Basic/TargetInfo.h"
5758
#include "clang/Basic/TargetOptions.h"
5859
#include "clang/Driver/Driver.h"
60+
5961
#include "llvm/ADT/ArrayRef.h"
6062
#include "llvm/ADT/StringRef.h"
6163
#include "llvm/ADT/StringSet.h"
@@ -996,21 +998,6 @@ static const char *getImportFailureString(swift::serialization::Status status) {
996998
}
997999
}
9981000

999-
/// Initialize the compiler invocation with it the search paths from a
1000-
/// serialized AST.
1001-
/// \returns true on success.
1002-
static bool DeserializeCompilerFlags(swift::CompilerInvocation &invocation,
1003-
StringRef section_data_ref, StringRef name,
1004-
llvm::raw_ostream &error) {
1005-
auto result = invocation.loadFromSerializedAST(section_data_ref);
1006-
if (result == swift::serialization::Status::Valid)
1007-
return true;
1008-
1009-
error << "Could not deserialize " << name << ":\n"
1010-
<< getImportFailureString(result) << "\n";
1011-
return false;
1012-
}
1013-
10141001
static void printASTValidationError(
10151002
llvm::raw_ostream &errs,
10161003
const swift::serialization::ValidationInfo &ast_info,
@@ -1057,6 +1044,7 @@ void SwiftASTContext::DiagnoseWarnings(Process &process, Module &module) const {
10571044
/// \returns true if an error was encountered.
10581045
static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation,
10591046
Module &module,
1047+
bool discover_implicit_search_paths,
10601048
const std::string &m_description,
10611049
llvm::raw_ostream &error,
10621050
bool &got_serialized_options,
@@ -1071,18 +1059,35 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation,
10711059
if (ast_file_datas.empty())
10721060
return false;
10731061

1062+
auto &search_path_options = invocation.getSearchPathOptions();
1063+
std::vector<std::string> import_search_paths;
1064+
llvm::StringSet<> known_import_search_paths;
1065+
for (auto &path : search_path_options.getImportSearchPaths()) {
1066+
import_search_paths.push_back(path);
1067+
known_import_search_paths.insert(path);
1068+
}
1069+
1070+
std::vector<swift::SearchPathOptions::FrameworkSearchPath>
1071+
framework_search_paths;
1072+
llvm::StringSet<> known_framework_search_paths;
1073+
for (auto &path : search_path_options.getFrameworkSearchPaths()) {
1074+
framework_search_paths.push_back(path);
1075+
known_framework_search_paths.insert(path.Path);
1076+
}
1077+
10741078
// An AST section consists of one or more AST modules, optionally
10751079
// with headers. Iterate over all AST modules.
10761080
for (auto ast_file_data_sp : ast_file_datas) {
10771081
llvm::StringRef buf((const char *)ast_file_data_sp->GetBytes(),
10781082
ast_file_data_sp->GetByteSize());
10791083
swift::serialization::ValidationInfo info;
10801084
for (; !buf.empty(); buf = buf.substr(info.bytes)) {
1085+
llvm::SmallVector<swift::serialization::SearchPath> searchPaths;
10811086
swift::serialization::ExtendedValidationInfo extended_validation_info;
10821087
info = swift::serialization::validateSerializedAST(
10831088
buf, invocation.getSILOptions().EnableOSSAModules,
10841089
/*requiredSDK*/ StringRef(), /*requiresRevisionMatch*/ false,
1085-
&extended_validation_info);
1090+
&extended_validation_info, /*dependencies*/ nullptr, &searchPaths);
10861091
bool invalid_ast = info.status != swift::serialization::Status::Valid;
10871092
bool invalid_size = (info.bytes == 0) || (info.bytes > buf.size());
10881093
bool invalid_name = info.name.empty();
@@ -1099,15 +1104,54 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation,
10991104

11001105
found_swift_modules = true;
11011106
StringRef moduleData = buf.substr(0, info.bytes);
1102-
got_serialized_options |=
1103-
DeserializeCompilerFlags(invocation, moduleData, info.name, error);
1107+
1108+
auto remap = [&](std::string path) {
1109+
ConstString remapped;
1110+
if (module.GetSourceMappingList().RemapPath(ConstString(path),
1111+
remapped))
1112+
return remapped.GetStringRef().str();
1113+
return path;
1114+
};
1115+
1116+
/// Initialize the compiler invocation with it the search paths from a
1117+
/// serialized AST.
1118+
auto deserializeCompilerFlags = [&]() -> bool {
1119+
auto result = invocation.loadFromSerializedAST(moduleData);
1120+
if (result == swift::serialization::Status::Valid) {
1121+
if (discover_implicit_search_paths) {
1122+
for (auto &searchPath : searchPaths) {
1123+
std::string path = remap(searchPath.Path);
1124+
if (!searchPath.IsFramework) {
1125+
if (known_import_search_paths.insert(path).second)
1126+
import_search_paths.push_back(path);
1127+
} else {
1128+
swift::SearchPathOptions::FrameworkSearchPath
1129+
framework_search_path(path, searchPath.IsSystem);
1130+
if (known_framework_search_paths.insert(path).second)
1131+
framework_search_paths.push_back(framework_search_path);
1132+
}
1133+
}
1134+
}
1135+
return true;
1136+
}
1137+
1138+
error << "Could not deserialize " << info.name << ":\n"
1139+
<< getImportFailureString(result) << "\n";
1140+
return false;
1141+
};
1142+
1143+
got_serialized_options |= deserializeCompilerFlags();
1144+
11041145
LOG_PRINTF(
11051146
GetLog(LLDBLog::Types), "SDK path from module \"%s\" was \"%s\".",
11061147
info.name.str().c_str(), invocation.getSDKPath().str().c_str());
11071148
// We will deduce a matching SDK path from DWARF later.
11081149
invocation.setSDKPath("");
11091150
}
11101151
}
1152+
1153+
search_path_options.setImportSearchPaths(import_search_paths);
1154+
search_path_options.setFrameworkSearchPaths(framework_search_paths);
11111155
return found_validation_errors;
11121156
}
11131157

@@ -1503,9 +1547,12 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
15031547
bool got_serialized_options = false;
15041548
llvm::SmallString<0> error;
15051549
llvm::raw_svector_ostream errs(error);
1506-
if (DeserializeAllCompilerFlags(
1507-
swift_ast_sp->GetCompilerInvocation(), module, m_description, errs,
1508-
got_serialized_options, found_swift_modules)) {
1550+
// Implicit search paths will be discoverd by ValidateSecionModules().
1551+
bool discover_implicit_search_paths = false;
1552+
if (DeserializeAllCompilerFlags(swift_ast_sp->GetCompilerInvocation(),
1553+
module, discover_implicit_search_paths,
1554+
m_description, errs, got_serialized_options,
1555+
found_swift_modules)) {
15091556
// Validation errors are not fatal for the context.
15101557
swift_ast_sp->m_module_import_warnings.push_back(std::string(error));
15111558
}
@@ -1811,9 +1858,9 @@ ProcessModule(ModuleSP module_sp, std::string m_description,
18111858
llvm::SmallString<0> error;
18121859
llvm::raw_svector_ostream errs(error);
18131860
swift::CompilerInvocation invocation;
1814-
if (DeserializeAllCompilerFlags(invocation, *module_sp, m_description, errs,
1815-
got_serialized_options,
1816-
found_swift_modules)) {
1861+
if (DeserializeAllCompilerFlags(
1862+
invocation, *module_sp, discover_implicit_search_paths, m_description,
1863+
errs, got_serialized_options, found_swift_modules)) {
18171864
// TODO: After removing DeserializeAllCompilerFlags from
18181865
// CreateInstance(per-Module), errs will need to be
18191866
// collected here and surfaced.
@@ -1823,53 +1870,14 @@ ProcessModule(ModuleSP module_sp, std::string m_description,
18231870
module_search_paths.insert(module_search_paths.end(),
18241871
opts.getImportSearchPaths().begin(),
18251872
opts.getImportSearchPaths().end());
1873+
for (auto path:opts.getFrameworkSearchPaths())
1874+
framework_search_paths.push_back({path.Path, path.IsSystem});
18261875
auto &clang_opts = invocation.getClangImporterOptions().ExtraArgs;
18271876
for (const std::string &arg : clang_opts) {
18281877
extra_clang_args.push_back(arg);
18291878
LOG_VERBOSE_PRINTF(GetLog(LLDBLog::Types), "adding Clang argument \"%s\".",
18301879
arg.c_str());
18311880
}
1832-
// FIXME: Unfortunately this:
1833-
//
1834-
// for (const auto &fwsp : opts.getFrameworkSearchPaths())
1835-
// framework_search_paths.push_back({fwsp.Path, fwsp.IsSystem});
1836-
//
1837-
// is insufficient, as ClangImporter can discover more framework
1838-
// search paths on the fly. Once a better solution is found,
1839-
// warmup_contexts can be retired (again).
1840-
{
1841-
SymbolFile *sym_file = module_sp->GetSymbolFile();
1842-
if (!sym_file)
1843-
return;
1844-
Status sym_file_error;
1845-
auto type_system_or_err =
1846-
sym_file->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
1847-
if (!type_system_or_err) {
1848-
llvm::consumeError(type_system_or_err.takeError());
1849-
return;
1850-
}
1851-
auto ts =
1852-
llvm::dyn_cast_or_null<TypeSystemSwift>(type_system_or_err->get());
1853-
if (!ts)
1854-
return;
1855-
1856-
SwiftASTContext *ast_context = ts->GetSwiftASTContext();
1857-
if (!ast_context || ast_context->HasErrors())
1858-
return;
1859-
1860-
if (discover_implicit_search_paths) {
1861-
const auto &opts = ast_context->GetSearchPathOptions();
1862-
for (const auto &isp : opts.getImportSearchPaths())
1863-
module_search_paths.push_back(isp);
1864-
}
1865-
1866-
if (use_all_compiler_flags ||
1867-
target.GetExecutableModulePointer() == module_sp.get()) {
1868-
const auto &opts = ast_context->GetSearchPathOptions();
1869-
for (const auto &fwsp : opts.getFrameworkSearchPaths())
1870-
framework_search_paths.push_back({fwsp.Path, fwsp.IsSystem});
1871-
}
1872-
}
18731881
}
18741882

18751883
lldb::TypeSystemSP SwiftASTContext::CreateInstance(

0 commit comments

Comments
 (0)