Skip to content

Commit 7a97645

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 (cherry picked from commit 7c592dc)
1 parent dec5c46 commit 7a97645

File tree

1 file changed

+74
-65
lines changed

1 file changed

+74
-65
lines changed

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

Lines changed: 74 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"
@@ -995,21 +997,6 @@ static const char *getImportFailureString(swift::serialization::Status status) {
995997
}
996998
}
997999

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

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

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

@@ -1513,9 +1558,12 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
15131558
bool got_serialized_options = false;
15141559
llvm::SmallString<0> error;
15151560
llvm::raw_svector_ostream errs(error);
1516-
if (DeserializeAllCompilerFlags(
1517-
swift_ast_sp->GetCompilerInvocation(), module, m_description, errs,
1518-
got_serialized_options, found_swift_modules)) {
1561+
// Implicit search paths will be discoverd by ValidateSecionModules().
1562+
bool discover_implicit_search_paths = false;
1563+
if (DeserializeAllCompilerFlags(swift_ast_sp->GetCompilerInvocation(),
1564+
module, discover_implicit_search_paths,
1565+
m_description, errs, got_serialized_options,
1566+
found_swift_modules)) {
15191567
// Validation errors are not fatal for the context.
15201568
swift_ast_sp->m_module_import_warnings.push_back(std::string(error));
15211569
}
@@ -1821,9 +1869,9 @@ ProcessModule(ModuleSP module_sp, std::string m_description,
18211869
llvm::SmallString<0> error;
18221870
llvm::raw_svector_ostream errs(error);
18231871
swift::CompilerInvocation invocation;
1824-
if (DeserializeAllCompilerFlags(invocation, *module_sp, m_description, errs,
1825-
got_serialized_options,
1826-
found_swift_modules)) {
1872+
if (DeserializeAllCompilerFlags(
1873+
invocation, *module_sp, discover_implicit_search_paths, m_description,
1874+
errs, got_serialized_options, found_swift_modules)) {
18271875
// TODO: After removing DeserializeAllCompilerFlags from
18281876
// CreateInstance(per-Module), errs will need to be
18291877
// collected here and surfaced.
@@ -1833,53 +1881,14 @@ ProcessModule(ModuleSP module_sp, std::string m_description,
18331881
module_search_paths.insert(module_search_paths.end(),
18341882
opts.getImportSearchPaths().begin(),
18351883
opts.getImportSearchPaths().end());
1884+
for (auto path:opts.getFrameworkSearchPaths())
1885+
framework_search_paths.push_back({path.Path, path.IsSystem});
18361886
auto &clang_opts = invocation.getClangImporterOptions().ExtraArgs;
18371887
for (const std::string &arg : clang_opts) {
18381888
extra_clang_args.push_back(arg);
18391889
LOG_VERBOSE_PRINTF(GetLog(LLDBLog::Types), "adding Clang argument \"%s\".",
18401890
arg.c_str());
18411891
}
1842-
// FIXME: Unfortunately this:
1843-
//
1844-
// for (const auto &fwsp : opts.getFrameworkSearchPaths())
1845-
// framework_search_paths.push_back({fwsp.Path, fwsp.IsSystem});
1846-
//
1847-
// is insufficient, as ClangImporter can discover more framework
1848-
// search paths on the fly. Once a better solution is found,
1849-
// warmup_contexts can be retired (again).
1850-
{
1851-
SymbolFile *sym_file = module_sp->GetSymbolFile();
1852-
if (!sym_file)
1853-
return;
1854-
Status sym_file_error;
1855-
auto type_system_or_err =
1856-
sym_file->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
1857-
if (!type_system_or_err) {
1858-
llvm::consumeError(type_system_or_err.takeError());
1859-
return;
1860-
}
1861-
auto ts =
1862-
llvm::dyn_cast_or_null<TypeSystemSwift>(type_system_or_err->get());
1863-
if (!ts)
1864-
return;
1865-
1866-
SwiftASTContext *ast_context = ts->GetSwiftASTContext();
1867-
if (!ast_context || ast_context->HasErrors())
1868-
return;
1869-
1870-
if (discover_implicit_search_paths) {
1871-
const auto &opts = ast_context->GetSearchPathOptions();
1872-
for (const auto &isp : opts.getImportSearchPaths())
1873-
module_search_paths.push_back(isp);
1874-
}
1875-
1876-
if (use_all_compiler_flags ||
1877-
target.GetExecutableModulePointer() == module_sp.get()) {
1878-
const auto &opts = ast_context->GetSearchPathOptions();
1879-
for (const auto &fwsp : opts.getFrameworkSearchPaths())
1880-
framework_search_paths.push_back({fwsp.Path, fwsp.IsSystem});
1881-
}
1882-
}
18831892
}
18841893

18851894
lldb::TypeSystemSP SwiftASTContext::CreateInstance(

0 commit comments

Comments
 (0)