Skip to content

Commit 9a27fd5

Browse files
committed
Revert "Move deserialization of import search paths into the scratch context"
This reverts commit 7a97645.
1 parent 7a97645 commit 9a27fd5

File tree

1 file changed

+65
-74
lines changed

1 file changed

+65
-74
lines changed

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

Lines changed: 65 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,10 @@
5252
#include "swift/Sema/IDETypeChecking.h"
5353
#include "swift/Serialization/Validation.h"
5454
#include "swift/SymbolGraphGen/SymbolGraphOptions.h"
55-
5655
#include "clang/AST/ASTContext.h"
5756
#include "clang/Basic/TargetInfo.h"
5857
#include "clang/Basic/TargetOptions.h"
5958
#include "clang/Driver/Driver.h"
60-
6159
#include "llvm/ADT/ArrayRef.h"
6260
#include "llvm/ADT/StringRef.h"
6361
#include "llvm/ADT/StringSet.h"
@@ -997,6 +995,21 @@ static const char *getImportFailureString(swift::serialization::Status status) {
997995
}
998996
}
999997

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+
10001013
static void printASTValidationError(
10011014
llvm::raw_ostream &errs,
10021015
const swift::serialization::ValidationInfo &ast_info,
@@ -1043,7 +1056,6 @@ void SwiftASTContext::DiagnoseWarnings(Process &process, Module &module) const {
10431056
/// \returns true if an error was encountered.
10441057
static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation,
10451058
Module &module,
1046-
bool discover_implicit_search_paths,
10471059
const std::string &m_description,
10481060
llvm::raw_ostream &error,
10491061
bool &got_serialized_options,
@@ -1058,35 +1070,17 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation,
10581070
if (ast_file_datas.empty())
10591071
return false;
10601072

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-
10771073
// An AST section consists of one or more AST modules, optionally
10781074
// with headers. Iterate over all AST modules.
10791075
for (auto ast_file_data_sp : ast_file_datas) {
10801076
llvm::StringRef buf((const char *)ast_file_data_sp->GetBytes(),
10811077
ast_file_data_sp->GetByteSize());
10821078
swift::serialization::ValidationInfo info;
10831079
for (; !buf.empty(); buf = buf.substr(info.bytes)) {
1084-
llvm::SmallVector<swift::serialization::SearchPath> searchPaths;
10851080
swift::serialization::ExtendedValidationInfo extended_validation_info;
10861081
info = swift::serialization::validateSerializedAST(
10871082
buf, invocation.getSILOptions().EnableOSSAModules,
1088-
/*requiredSDK*/ StringRef(), /*requiresRevisionMatch*/ false,
1089-
&extended_validation_info, /*dependencies*/ nullptr, &searchPaths);
1083+
/*requiredSDK*/StringRef(), &extended_validation_info);
10901084
bool invalid_ast = info.status != swift::serialization::Status::Valid;
10911085
bool invalid_size = (info.bytes == 0) || (info.bytes > buf.size());
10921086
bool invalid_name = info.name.empty();
@@ -1103,54 +1097,15 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation,
11031097

11041098
found_swift_modules = true;
11051099
StringRef moduleData = buf.substr(0, info.bytes);
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-
1100+
got_serialized_options |=
1101+
DeserializeCompilerFlags(invocation, moduleData, info.name, error);
11441102
LOG_PRINTF(
11451103
GetLog(LLDBLog::Types), "SDK path from module \"%s\" was \"%s\".",
11461104
info.name.str().c_str(), invocation.getSDKPath().str().c_str());
11471105
// We will deduce a matching SDK path from DWARF later.
11481106
invocation.setSDKPath("");
11491107
}
11501108
}
1151-
1152-
search_path_options.setImportSearchPaths(import_search_paths);
1153-
search_path_options.setFrameworkSearchPaths(framework_search_paths);
11541109
return found_validation_errors;
11551110
}
11561111

@@ -1558,12 +1513,9 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
15581513
bool got_serialized_options = false;
15591514
llvm::SmallString<0> error;
15601515
llvm::raw_svector_ostream errs(error);
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)) {
1516+
if (DeserializeAllCompilerFlags(
1517+
swift_ast_sp->GetCompilerInvocation(), module, m_description, errs,
1518+
got_serialized_options, found_swift_modules)) {
15671519
// Validation errors are not fatal for the context.
15681520
swift_ast_sp->m_module_import_warnings.push_back(std::string(error));
15691521
}
@@ -1869,9 +1821,9 @@ ProcessModule(ModuleSP module_sp, std::string m_description,
18691821
llvm::SmallString<0> error;
18701822
llvm::raw_svector_ostream errs(error);
18711823
swift::CompilerInvocation invocation;
1872-
if (DeserializeAllCompilerFlags(
1873-
invocation, *module_sp, discover_implicit_search_paths, m_description,
1874-
errs, got_serialized_options, found_swift_modules)) {
1824+
if (DeserializeAllCompilerFlags(invocation, *module_sp, m_description, errs,
1825+
got_serialized_options,
1826+
found_swift_modules)) {
18751827
// TODO: After removing DeserializeAllCompilerFlags from
18761828
// CreateInstance(per-Module), errs will need to be
18771829
// collected here and surfaced.
@@ -1881,14 +1833,53 @@ ProcessModule(ModuleSP module_sp, std::string m_description,
18811833
module_search_paths.insert(module_search_paths.end(),
18821834
opts.getImportSearchPaths().begin(),
18831835
opts.getImportSearchPaths().end());
1884-
for (auto path:opts.getFrameworkSearchPaths())
1885-
framework_search_paths.push_back({path.Path, path.IsSystem});
18861836
auto &clang_opts = invocation.getClangImporterOptions().ExtraArgs;
18871837
for (const std::string &arg : clang_opts) {
18881838
extra_clang_args.push_back(arg);
18891839
LOG_VERBOSE_PRINTF(GetLog(LLDBLog::Types), "adding Clang argument \"%s\".",
18901840
arg.c_str());
18911841
}
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+
}
18921883
}
18931884

18941885
lldb::TypeSystemSP SwiftASTContext::CreateInstance(

0 commit comments

Comments
 (0)