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