52
52
#include " swift/Sema/IDETypeChecking.h"
53
53
#include " swift/Serialization/Validation.h"
54
54
#include " swift/SymbolGraphGen/SymbolGraphOptions.h"
55
-
56
55
#include " clang/AST/ASTContext.h"
57
56
#include " clang/Basic/TargetInfo.h"
58
57
#include " clang/Basic/TargetOptions.h"
59
58
#include " clang/Driver/Driver.h"
60
-
61
59
#include " llvm/ADT/ArrayRef.h"
62
60
#include " llvm/ADT/StringRef.h"
63
61
#include " llvm/ADT/StringSet.h"
@@ -997,6 +995,21 @@ static const char *getImportFailureString(swift::serialization::Status status) {
997
995
}
998
996
}
999
997
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
+
1000
1013
static void printASTValidationError (
1001
1014
llvm::raw_ostream &errs,
1002
1015
const swift::serialization::ValidationInfo &ast_info,
@@ -1043,7 +1056,6 @@ void SwiftASTContext::DiagnoseWarnings(Process &process, Module &module) const {
1043
1056
// / \returns true if an error was encountered.
1044
1057
static bool DeserializeAllCompilerFlags (swift::CompilerInvocation &invocation,
1045
1058
Module &module ,
1046
- bool discover_implicit_search_paths,
1047
1059
const std::string &m_description,
1048
1060
llvm::raw_ostream &error,
1049
1061
bool &got_serialized_options,
@@ -1058,35 +1070,17 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation,
1058
1070
if (ast_file_datas.empty ())
1059
1071
return false ;
1060
1072
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
-
1077
1073
// An AST section consists of one or more AST modules, optionally
1078
1074
// with headers. Iterate over all AST modules.
1079
1075
for (auto ast_file_data_sp : ast_file_datas) {
1080
1076
llvm::StringRef buf ((const char *)ast_file_data_sp->GetBytes (),
1081
1077
ast_file_data_sp->GetByteSize ());
1082
1078
swift::serialization::ValidationInfo info;
1083
1079
for (; !buf.empty (); buf = buf.substr (info.bytes )) {
1084
- llvm::SmallVector<swift::serialization::SearchPath> searchPaths;
1085
1080
swift::serialization::ExtendedValidationInfo extended_validation_info;
1086
1081
info = swift::serialization::validateSerializedAST (
1087
1082
buf, invocation.getSILOptions ().EnableOSSAModules ,
1088
- /* requiredSDK*/ StringRef (), /* requiresRevisionMatch*/ false ,
1089
- &extended_validation_info, /* dependencies*/ nullptr , &searchPaths);
1083
+ /* requiredSDK*/ StringRef (), &extended_validation_info);
1090
1084
bool invalid_ast = info.status != swift::serialization::Status::Valid;
1091
1085
bool invalid_size = (info.bytes == 0 ) || (info.bytes > buf.size ());
1092
1086
bool invalid_name = info.name .empty ();
@@ -1103,54 +1097,15 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation,
1103
1097
1104
1098
found_swift_modules = true ;
1105
1099
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);
1144
1102
LOG_PRINTF (
1145
1103
GetLog (LLDBLog::Types), " SDK path from module \" %s\" was \" %s\" ." ,
1146
1104
info.name .str ().c_str (), invocation.getSDKPath ().str ().c_str ());
1147
1105
// We will deduce a matching SDK path from DWARF later.
1148
1106
invocation.setSDKPath (" " );
1149
1107
}
1150
1108
}
1151
-
1152
- search_path_options.setImportSearchPaths (import_search_paths);
1153
- search_path_options.setFrameworkSearchPaths (framework_search_paths);
1154
1109
return found_validation_errors;
1155
1110
}
1156
1111
@@ -1558,12 +1513,9 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
1558
1513
bool got_serialized_options = false ;
1559
1514
llvm::SmallString<0 > error;
1560
1515
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)) {
1567
1519
// Validation errors are not fatal for the context.
1568
1520
swift_ast_sp->m_module_import_warnings .push_back (std::string (error));
1569
1521
}
@@ -1869,9 +1821,9 @@ ProcessModule(ModuleSP module_sp, std::string m_description,
1869
1821
llvm::SmallString<0 > error;
1870
1822
llvm::raw_svector_ostream errs (error);
1871
1823
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)) {
1875
1827
// TODO: After removing DeserializeAllCompilerFlags from
1876
1828
// CreateInstance(per-Module), errs will need to be
1877
1829
// collected here and surfaced.
@@ -1881,14 +1833,53 @@ ProcessModule(ModuleSP module_sp, std::string m_description,
1881
1833
module_search_paths.insert (module_search_paths.end (),
1882
1834
opts.getImportSearchPaths ().begin (),
1883
1835
opts.getImportSearchPaths ().end ());
1884
- for (auto path:opts.getFrameworkSearchPaths ())
1885
- framework_search_paths.push_back ({path.Path , path.IsSystem });
1886
1836
auto &clang_opts = invocation.getClangImporterOptions ().ExtraArgs ;
1887
1837
for (const std::string &arg : clang_opts) {
1888
1838
extra_clang_args.push_back (arg);
1889
1839
LOG_VERBOSE_PRINTF (GetLog (LLDBLog::Types), " adding Clang argument \" %s\" ." ,
1890
1840
arg.c_str ());
1891
1841
}
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
+ }
1892
1883
}
1893
1884
1894
1885
lldb::TypeSystemSP SwiftASTContext::CreateInstance (
0 commit comments