@@ -1085,7 +1085,7 @@ static void printASTValidationError(
1085
1085
llvm::raw_ostream &errs,
1086
1086
const swift::serialization::ValidationInfo &ast_info,
1087
1087
const swift::serialization::ExtendedValidationInfo &ext_ast_info,
1088
- Module & module , StringRef module_buf, bool invalid_name,
1088
+ StringRef module_name , StringRef module_buf, bool invalid_name,
1089
1089
bool invalid_size) {
1090
1090
const char *error = getImportFailureString (ast_info.status );
1091
1091
errs << " AST validation error" ;
@@ -1110,9 +1110,9 @@ static void printASTValidationError(
1110
1110
- SDK path: {7}
1111
1111
- Clang Importer Options:
1112
1112
)" ,
1113
- ast_info.name , module . GetSpecificationDescription () , error,
1114
- ast_info.targetTriple , ast_info.shortVersion , ast_info. bytes ,
1115
- module_buf. size (), ext_ast_info.getSDKPath ());
1113
+ ast_info.name , module_name , error, ast_info. targetTriple ,
1114
+ ast_info.shortVersion , ast_info.bytes , module_buf. size () ,
1115
+ ext_ast_info.getSDKPath ());
1116
1116
for (StringRef ExtraOpt : ext_ast_info.getExtraClangImporterOptions ())
1117
1117
LLDB_LOG (log, " -- {0}" , ExtraOpt);
1118
1118
}
@@ -1170,21 +1170,16 @@ static std::string GetPluginServerForSDK(llvm::StringRef sdk_path) {
1170
1170
// / invocation with the concatenated search paths from the blobs.
1171
1171
// / \returns true if an error was encountered.
1172
1172
static bool DeserializeAllCompilerFlags (swift::CompilerInvocation &invocation,
1173
- Module &module ,
1173
+ llvm::StringRef module_name,
1174
+ llvm::ArrayRef<StringRef> buffers,
1175
+ const PathMappingList &path_map,
1174
1176
bool discover_implicit_search_paths,
1175
1177
const std::string &m_description,
1176
1178
llvm::raw_ostream &error,
1177
1179
bool &got_serialized_options,
1178
1180
bool &found_swift_modules) {
1179
1181
bool found_validation_errors = false ;
1180
1182
got_serialized_options = false ;
1181
- auto ast_file_datas = module .GetASTData (eLanguageTypeSwift);
1182
- LOG_PRINTF (GetLog (LLDBLog::Types), " Found %d AST file data entries." ,
1183
- (int )ast_file_datas.size ());
1184
-
1185
- // If no N_AST symbols exist, this is not an error.
1186
- if (ast_file_datas.empty ())
1187
- return false ;
1188
1183
1189
1184
auto &search_path_options = invocation.getSearchPathOptions ();
1190
1185
auto get_override_server = [&](llvm::StringRef plugin_path) -> std::string {
@@ -1257,9 +1252,7 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation,
1257
1252
1258
1253
// An AST section consists of one or more AST modules, optionally
1259
1254
// with headers. Iterate over all AST modules.
1260
- for (auto ast_file_data_sp : ast_file_datas) {
1261
- llvm::StringRef buf ((const char *)ast_file_data_sp->GetBytes (),
1262
- ast_file_data_sp->GetByteSize ());
1255
+ for (auto buf : buffers) {
1263
1256
swift::serialization::ValidationInfo info;
1264
1257
for (; !buf.empty (); buf = buf.substr (info.bytes )) {
1265
1258
llvm::SmallVector<swift::serialization::SearchPath> searchPaths;
@@ -1274,8 +1267,8 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation,
1274
1267
if (invalid_ast || invalid_size || invalid_name) {
1275
1268
// Validation errors are diagnosed, but not fatal for the context.
1276
1269
found_validation_errors = true ;
1277
- printASTValidationError (error, info, extended_validation_info, module ,
1278
- buf, invalid_name, invalid_size);
1270
+ printASTValidationError (error, info, extended_validation_info,
1271
+ module_name, buf, invalid_name, invalid_size);
1279
1272
// If there's a size error, quit the loop early, otherwise try the next.
1280
1273
if (invalid_size)
1281
1274
break ;
@@ -1287,8 +1280,7 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation,
1287
1280
1288
1281
auto remap = [&](const std::string &path) {
1289
1282
ConstString remapped;
1290
- if (module .GetSourceMappingList ().RemapPath (ConstString (path),
1291
- remapped))
1283
+ if (path_map.RemapPath (ConstString (path), remapped))
1292
1284
return remapped.GetStringRef ().str ();
1293
1285
return path;
1294
1286
};
@@ -1761,17 +1753,37 @@ static std::string GetSDKPathFromDebugInfo(std::string m_description,
1761
1753
return GetSDKPath (m_description, sdk);
1762
1754
}
1763
1755
1756
+ static std::vector<llvm::StringRef>
1757
+ GetASTBuffersFromModule (const std::string &m_description,
1758
+ llvm::ArrayRef<lldb::DataBufferSP> ast_file_datas,
1759
+ std::string &module_name) {
1760
+ LOG_PRINTF (GetLog (LLDBLog::Types), " Found %d AST file data entries in %s." ,
1761
+ (int )ast_file_datas.size (), module_name.c_str ());
1762
+ std::vector<llvm::StringRef> buffers;
1763
+ for (auto &data : ast_file_datas)
1764
+ if (data)
1765
+ buffers.push_back (
1766
+ StringRef ((const char *)data->GetBytes (), data->GetByteSize ()));
1767
+ return buffers;
1768
+ }
1769
+
1764
1770
// / Detect whether a Swift module was "imported" by DWARFImporter.
1765
1771
// / All this *really* means is that it couldn't be loaded through any
1766
1772
// / other mechanism.
1767
1773
static bool IsDWARFImported (swift::ModuleDecl &module ) {
1768
- return std::any_of (module .getFiles ().begin (), module .getFiles ().end (),
1769
- [](swift::FileUnit *file_unit) {
1770
- return (file_unit->getKind () ==
1771
- swift::FileUnitKind::DWARFModule);
1772
- });
1774
+ return llvm::any_of (module .getFiles (), [](swift::FileUnit *file_unit) {
1775
+ return (file_unit->getKind () == swift::FileUnitKind::DWARFModule);
1776
+ });
1777
+ }
1778
+
1779
+ // / Detect whether this is a proper Swift module.
1780
+ static bool IsSerializedAST (swift::ModuleDecl &module ) {
1781
+ return llvm::any_of (module .getFiles (), [](swift::FileUnit *file_unit) {
1782
+ return (file_unit->getKind () == swift::FileUnitKind::SerializedAST);
1783
+ });
1773
1784
}
1774
1785
1786
+
1775
1787
lldb::TypeSystemSP
1776
1788
SwiftASTContext::CreateInstance (lldb::LanguageType language, Module &module ,
1777
1789
TypeSystemSwiftTypeRef &typeref_typesystem,
@@ -1878,15 +1890,24 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
1878
1890
bool got_serialized_options = false ;
1879
1891
llvm::SmallString<0 > error;
1880
1892
llvm::raw_svector_ostream errs (error);
1881
- // Implicit search paths will be discoverd by ValidateSecionModules().
1893
+ // Implicit search paths will be discovered by ValidateSecionModules().
1882
1894
bool discover_implicit_search_paths = false ;
1883
- if (DeserializeAllCompilerFlags (swift_ast_sp->GetCompilerInvocation (),
1884
- module , discover_implicit_search_paths,
1885
- m_description, errs, got_serialized_options,
1886
- found_swift_modules)) {
1887
- // Validation errors are not fatal for the context.
1888
- swift_ast_sp->AddDiagnostic (eDiagnosticSeverityError, errs.str ());
1889
- }
1895
+ auto ast_file_datas = module .GetASTData (eLanguageTypeSwift);
1896
+ std::string module_name = module .GetSpecificationDescription ();
1897
+ std::vector<llvm::StringRef> buffers =
1898
+ GetASTBuffersFromModule (m_description, ast_file_datas, module_name);
1899
+
1900
+ // If no N_AST symbols exist, this is not an error.
1901
+ if (!buffers.empty ())
1902
+ if (DeserializeAllCompilerFlags (
1903
+ swift_ast_sp->GetCompilerInvocation (),
1904
+ module_name, buffers,
1905
+ module .GetSourceMappingList (), discover_implicit_search_paths,
1906
+ m_description, errs, got_serialized_options,
1907
+ found_swift_modules)) {
1908
+ // Validation errors are not fatal for the context.
1909
+ swift_ast_sp->AddDiagnostic (eDiagnosticSeverityError, errs.str ());
1910
+ }
1890
1911
1891
1912
llvm::StringRef serialized_triple =
1892
1913
swift_ast_sp->GetCompilerInvocation ().getTargetTriple ();
@@ -2180,13 +2201,21 @@ static void ProcessModule(
2180
2201
llvm::SmallString<0 > error;
2181
2202
llvm::raw_svector_ostream errs (error);
2182
2203
swift::CompilerInvocation invocation;
2183
- if (DeserializeAllCompilerFlags (
2184
- invocation, *module_sp, discover_implicit_search_paths, m_description,
2185
- errs, got_serialized_options, found_swift_modules)) {
2186
- // TODO: After removing DeserializeAllCompilerFlags from
2187
- // CreateInstance(per-Module), errs will need to be
2188
- // collected here and surfaced.
2189
- }
2204
+ auto ast_file_datas = module_sp->GetASTData (eLanguageTypeSwift);
2205
+ std::string module_name = module_sp->GetSpecificationDescription ();
2206
+ std::vector<llvm::StringRef> buffers =
2207
+ GetASTBuffersFromModule (m_description, ast_file_datas, module_name);
2208
+
2209
+ // If no N_AST symbols exist, this is not an error.
2210
+ if (!buffers.empty ())
2211
+ if (DeserializeAllCompilerFlags (
2212
+ invocation, module_name, buffers, module_sp->GetSourceMappingList (),
2213
+ discover_implicit_search_paths, m_description, errs,
2214
+ got_serialized_options, found_swift_modules)) {
2215
+ // TODO: After removing DeserializeAllCompilerFlags from
2216
+ // CreateInstance(per-Module), errs will need to be
2217
+ // collected here and surfaced.
2218
+ }
2190
2219
2191
2220
// Copy the interesting deserialized flags to the out parameters.
2192
2221
const auto &opts = invocation.getSearchPathOptions ();
@@ -8316,10 +8345,42 @@ bool SwiftASTContextForExpressions::CacheUserImports(
8316
8345
LOG_PRINTF (GetLog (LLDBLog::Types | LLDBLog::Expressions),
8317
8346
" Performing auto import on found module: %s.\n " ,
8318
8347
module_name.c_str ());
8319
- if (!LoadOneModule (module_info, *this , process_sp,
8320
- /* import_dylibs=*/ true , error))
8348
+ auto *module_decl = LoadOneModule (module_info, *this , process_sp,
8349
+ /* import_dylibs=*/ true , error);
8350
+ if (!module_decl)
8321
8351
return false ;
8322
-
8352
+ if (IsSerializedAST (*module_decl)) {
8353
+ // Parse additional search paths from the module.
8354
+ StringRef ast_file = module_decl->getModuleLoadedFilename ();
8355
+ if (llvm::sys::path::is_absolute (ast_file)) {
8356
+ auto file_or_err =
8357
+ llvm::MemoryBuffer::getFile (ast_file, /* IsText=*/ false ,
8358
+ /* RequiresNullTerminator=*/ false );
8359
+ if (!file_or_err.getError () && file_or_err->get ()) {
8360
+ PathMappingList path_remap;
8361
+ llvm::SmallString<0 > error;
8362
+ bool found_swift_modules = false ;
8363
+ bool got_serialized_options = false ;
8364
+ llvm::raw_svector_ostream errs (error);
8365
+ bool discover_implicit_search_paths = false ;
8366
+ swift::CompilerInvocation &invocation = GetCompilerInvocation ();
8367
+
8368
+ LOG_PRINTF (GetLog (LLDBLog::Types),
8369
+ " Scanning for search paths in %s" ,
8370
+ ast_file.str ().c_str ());
8371
+ if (DeserializeAllCompilerFlags (
8372
+ invocation, ast_file, {file_or_err->get ()->getBuffer ()},
8373
+ path_remap, discover_implicit_search_paths,
8374
+ m_description.str ().str (), errs, got_serialized_options,
8375
+ found_swift_modules)) {
8376
+ LOG_PRINTF (GetLog (LLDBLog::Types), " Could not parse %s: %s" ,
8377
+ ast_file.str ().c_str (), error.str ().str ().c_str ());
8378
+ }
8379
+ if (got_serialized_options)
8380
+ LogConfiguration ();
8381
+ }
8382
+ }
8383
+ }
8323
8384
// How do we tell we are in REPL or playground mode?
8324
8385
AddHandLoadedModule (module_const_str, attributed_import);
8325
8386
}
0 commit comments