52
52
#include " swift/Sema/IDETypeChecking.h"
53
53
#include " swift/Serialization/Validation.h"
54
54
#include " swift/SymbolGraphGen/SymbolGraphOptions.h"
55
+
55
56
#include " clang/AST/ASTContext.h"
56
57
#include " clang/Basic/TargetInfo.h"
57
58
#include " clang/Basic/TargetOptions.h"
58
59
#include " clang/Driver/Driver.h"
60
+
59
61
#include " llvm/ADT/ArrayRef.h"
60
62
#include " llvm/ADT/StringRef.h"
61
63
#include " llvm/ADT/StringSet.h"
@@ -996,21 +998,6 @@ static const char *getImportFailureString(swift::serialization::Status status) {
996
998
}
997
999
}
998
1000
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
-
1014
1001
static void printASTValidationError (
1015
1002
llvm::raw_ostream &errs,
1016
1003
const swift::serialization::ValidationInfo &ast_info,
@@ -1057,6 +1044,7 @@ void SwiftASTContext::DiagnoseWarnings(Process &process, Module &module) const {
1057
1044
// / \returns true if an error was encountered.
1058
1045
static bool DeserializeAllCompilerFlags (swift::CompilerInvocation &invocation,
1059
1046
Module &module ,
1047
+ bool discover_implicit_search_paths,
1060
1048
const std::string &m_description,
1061
1049
llvm::raw_ostream &error,
1062
1050
bool &got_serialized_options,
@@ -1071,18 +1059,35 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation,
1071
1059
if (ast_file_datas.empty ())
1072
1060
return false ;
1073
1061
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
+
1074
1078
// An AST section consists of one or more AST modules, optionally
1075
1079
// with headers. Iterate over all AST modules.
1076
1080
for (auto ast_file_data_sp : ast_file_datas) {
1077
1081
llvm::StringRef buf ((const char *)ast_file_data_sp->GetBytes (),
1078
1082
ast_file_data_sp->GetByteSize ());
1079
1083
swift::serialization::ValidationInfo info;
1080
1084
for (; !buf.empty (); buf = buf.substr (info.bytes )) {
1085
+ llvm::SmallVector<swift::serialization::SearchPath> searchPaths;
1081
1086
swift::serialization::ExtendedValidationInfo extended_validation_info;
1082
1087
info = swift::serialization::validateSerializedAST (
1083
1088
buf, invocation.getSILOptions ().EnableOSSAModules ,
1084
1089
/* requiredSDK*/ StringRef (), /* requiresRevisionMatch*/ false ,
1085
- &extended_validation_info);
1090
+ &extended_validation_info, /* dependencies */ nullptr , &searchPaths );
1086
1091
bool invalid_ast = info.status != swift::serialization::Status::Valid;
1087
1092
bool invalid_size = (info.bytes == 0 ) || (info.bytes > buf.size ());
1088
1093
bool invalid_name = info.name .empty ();
@@ -1099,15 +1104,54 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation,
1099
1104
1100
1105
found_swift_modules = true ;
1101
1106
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
+
1104
1145
LOG_PRINTF (
1105
1146
GetLog (LLDBLog::Types), " SDK path from module \" %s\" was \" %s\" ." ,
1106
1147
info.name .str ().c_str (), invocation.getSDKPath ().str ().c_str ());
1107
1148
// We will deduce a matching SDK path from DWARF later.
1108
1149
invocation.setSDKPath (" " );
1109
1150
}
1110
1151
}
1152
+
1153
+ search_path_options.setImportSearchPaths (import_search_paths);
1154
+ search_path_options.setFrameworkSearchPaths (framework_search_paths);
1111
1155
return found_validation_errors;
1112
1156
}
1113
1157
@@ -1503,9 +1547,12 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
1503
1547
bool got_serialized_options = false ;
1504
1548
llvm::SmallString<0 > error;
1505
1549
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)) {
1509
1556
// Validation errors are not fatal for the context.
1510
1557
swift_ast_sp->m_module_import_warnings .push_back (std::string (error));
1511
1558
}
@@ -1811,9 +1858,9 @@ ProcessModule(ModuleSP module_sp, std::string m_description,
1811
1858
llvm::SmallString<0 > error;
1812
1859
llvm::raw_svector_ostream errs (error);
1813
1860
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)) {
1817
1864
// TODO: After removing DeserializeAllCompilerFlags from
1818
1865
// CreateInstance(per-Module), errs will need to be
1819
1866
// collected here and surfaced.
@@ -1823,53 +1870,14 @@ ProcessModule(ModuleSP module_sp, std::string m_description,
1823
1870
module_search_paths.insert (module_search_paths.end (),
1824
1871
opts.getImportSearchPaths ().begin (),
1825
1872
opts.getImportSearchPaths ().end ());
1873
+ for (auto path:opts.getFrameworkSearchPaths ())
1874
+ framework_search_paths.push_back ({path.Path , path.IsSystem });
1826
1875
auto &clang_opts = invocation.getClangImporterOptions ().ExtraArgs ;
1827
1876
for (const std::string &arg : clang_opts) {
1828
1877
extra_clang_args.push_back (arg);
1829
1878
LOG_VERBOSE_PRINTF (GetLog (LLDBLog::Types), " adding Clang argument \" %s\" ." ,
1830
1879
arg.c_str ());
1831
1880
}
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
- }
1873
1881
}
1874
1882
1875
1883
lldb::TypeSystemSP SwiftASTContext::CreateInstance (
0 commit comments