@@ -226,30 +226,26 @@ template <typename Instance> class PluginInstances {
226
226
}
227
227
228
228
typename Instance::CallbackType GetCallbackAtIndex (uint32_t idx) {
229
- if (Instance *instance = GetInstanceAtIndex (idx))
229
+ if (const Instance *instance = GetInstanceAtIndex (idx))
230
230
return instance->create_callback ;
231
231
return nullptr ;
232
232
}
233
233
234
234
llvm::StringRef GetDescriptionAtIndex (uint32_t idx) {
235
- if (Instance *instance = GetInstanceAtIndex (idx))
235
+ if (const Instance *instance = GetInstanceAtIndex (idx))
236
236
return instance->description ;
237
237
return " " ;
238
238
}
239
239
240
240
llvm::StringRef GetNameAtIndex (uint32_t idx) {
241
- if (Instance *instance = GetInstanceAtIndex (idx))
241
+ if (const Instance *instance = GetInstanceAtIndex (idx))
242
242
return instance->name ;
243
243
return " " ;
244
244
}
245
245
246
246
typename Instance::CallbackType GetCallbackForName (llvm::StringRef name) {
247
- if (name.empty ())
248
- return nullptr ;
249
- for (auto &instance : m_instances) {
250
- if (name == instance.name )
251
- return instance.create_callback ;
252
- }
247
+ if (const Instance *instance = GetInstanceForName (name))
248
+ return instance->create_callback ;
253
249
return nullptr ;
254
250
}
255
251
@@ -260,12 +256,33 @@ template <typename Instance> class PluginInstances {
260
256
}
261
257
}
262
258
263
- const std::vector<Instance> &GetInstances () const { return m_instances; }
264
- std::vector<Instance> &GetInstances () { return m_instances; }
259
+ // Return a copy of all the enabled instances.
260
+ // Note that this is a copy of the internal state so modifications
261
+ // to the returned instances will not be reflected back to instances
262
+ // stored by the PluginInstances object.
263
+ std::vector<Instance> GetSnapshot () { return m_instances; }
264
+
265
+ const Instance *GetInstanceAtIndex (uint32_t idx) {
266
+ uint32_t count = 0 ;
267
+
268
+ return FindEnabledInstance (
269
+ [&](const Instance &instance) { return count++ == idx; });
270
+ }
271
+
272
+ const Instance *GetInstanceForName (llvm::StringRef name) {
273
+ if (name.empty ())
274
+ return nullptr ;
265
275
266
- Instance *GetInstanceAtIndex (uint32_t idx) {
267
- if (idx < m_instances.size ())
268
- return &m_instances[idx];
276
+ return FindEnabledInstance (
277
+ [&](const Instance &instance) { return instance.name == name; });
278
+ }
279
+
280
+ const Instance *
281
+ FindEnabledInstance (std::function<bool (const Instance &)> predicate) const {
282
+ for (const auto &instance : m_instances) {
283
+ if (predicate (instance))
284
+ return &instance;
285
+ }
269
286
return nullptr ;
270
287
}
271
288
@@ -571,17 +588,15 @@ PluginManager::GetLanguageRuntimeCreateCallbackAtIndex(uint32_t idx) {
571
588
572
589
LanguageRuntimeGetCommandObject
573
590
PluginManager::GetLanguageRuntimeGetCommandObjectAtIndex (uint32_t idx) {
574
- const auto &instances = GetLanguageRuntimeInstances ().GetInstances ();
575
- if (idx < instances.size ())
576
- return instances[idx].command_callback ;
591
+ if (auto instance = GetLanguageRuntimeInstances ().GetInstanceAtIndex (idx))
592
+ return instance->command_callback ;
577
593
return nullptr ;
578
594
}
579
595
580
596
LanguageRuntimeGetExceptionPrecondition
581
597
PluginManager::GetLanguageRuntimeGetExceptionPreconditionAtIndex (uint32_t idx) {
582
- const auto &instances = GetLanguageRuntimeInstances ().GetInstances ();
583
- if (idx < instances.size ())
584
- return instances[idx].precondition_callback ;
598
+ if (auto instance = GetLanguageRuntimeInstances ().GetInstanceAtIndex (idx))
599
+ return instance->precondition_callback ;
585
600
return nullptr ;
586
601
}
587
602
@@ -643,12 +658,7 @@ bool PluginManager::IsRegisteredObjectFilePluginName(llvm::StringRef name) {
643
658
if (name.empty ())
644
659
return false ;
645
660
646
- const auto &instances = GetObjectFileInstances ().GetInstances ();
647
- for (auto &instance : instances) {
648
- if (instance.name == name)
649
- return true ;
650
- }
651
- return false ;
661
+ return GetObjectFileInstances ().GetInstanceForName (name) != nullptr ;
652
662
}
653
663
654
664
bool PluginManager::RegisterPlugin (
@@ -674,29 +684,24 @@ PluginManager::GetObjectFileCreateCallbackAtIndex(uint32_t idx) {
674
684
675
685
ObjectFileCreateMemoryInstance
676
686
PluginManager::GetObjectFileCreateMemoryCallbackAtIndex (uint32_t idx) {
677
- const auto &instances = GetObjectFileInstances ().GetInstances ();
678
- if (idx < instances.size ())
679
- return instances[idx].create_memory_callback ;
687
+ if (auto instance = GetObjectFileInstances ().GetInstanceAtIndex (idx))
688
+ return instance->create_memory_callback ;
680
689
return nullptr ;
681
690
}
682
691
683
692
ObjectFileGetModuleSpecifications
684
693
PluginManager::GetObjectFileGetModuleSpecificationsCallbackAtIndex (
685
694
uint32_t idx) {
686
- const auto &instances = GetObjectFileInstances ().GetInstances ();
687
- if (idx < instances.size ())
688
- return instances[idx].get_module_specifications ;
695
+ if (auto instance = GetObjectFileInstances ().GetInstanceAtIndex (idx))
696
+ return instance->get_module_specifications ;
689
697
return nullptr ;
690
698
}
691
699
692
700
ObjectFileCreateMemoryInstance
693
701
PluginManager::GetObjectFileCreateMemoryCallbackForPluginName (
694
702
llvm::StringRef name) {
695
- const auto &instances = GetObjectFileInstances ().GetInstances ();
696
- for (auto &instance : instances) {
697
- if (instance.name == name)
698
- return instance.create_memory_callback ;
699
- }
703
+ if (auto instance = GetObjectFileInstances ().GetInstanceForName (name))
704
+ return instance->create_memory_callback ;
700
705
return nullptr ;
701
706
}
702
707
@@ -729,7 +734,7 @@ Status PluginManager::SaveCore(const lldb::ProcessSP &process_sp,
729
734
730
735
// Fall back to object plugins.
731
736
const auto &plugin_name = options.GetPluginName ().value_or (" " );
732
- auto & instances = GetObjectFileInstances ().GetInstances ();
737
+ auto instances = GetObjectFileInstances ().GetSnapshot ();
733
738
for (auto &instance : instances) {
734
739
if (plugin_name.empty () || instance.name == plugin_name) {
735
740
if (instance.save_core && instance.save_core (process_sp, options, error))
@@ -791,18 +796,16 @@ PluginManager::GetObjectContainerCreateCallbackAtIndex(uint32_t idx) {
791
796
792
797
ObjectContainerCreateMemoryInstance
793
798
PluginManager::GetObjectContainerCreateMemoryCallbackAtIndex (uint32_t idx) {
794
- const auto &instances = GetObjectContainerInstances ().GetInstances ();
795
- if (idx < instances.size ())
796
- return instances[idx].create_memory_callback ;
799
+ if (auto instance = GetObjectContainerInstances ().GetInstanceAtIndex (idx))
800
+ return instance->create_memory_callback ;
797
801
return nullptr ;
798
802
}
799
803
800
804
ObjectFileGetModuleSpecifications
801
805
PluginManager::GetObjectContainerGetModuleSpecificationsCallbackAtIndex (
802
806
uint32_t idx) {
803
- const auto &instances = GetObjectContainerInstances ().GetInstances ();
804
- if (idx < instances.size ())
805
- return instances[idx].get_module_specifications ;
807
+ if (auto instance = GetObjectContainerInstances ().GetInstanceAtIndex (idx))
808
+ return instance->get_module_specifications ;
806
809
return nullptr ;
807
810
}
808
811
@@ -849,7 +852,7 @@ PluginManager::GetPlatformCreateCallbackForPluginName(llvm::StringRef name) {
849
852
850
853
void PluginManager::AutoCompletePlatformName (llvm::StringRef name,
851
854
CompletionRequest &request) {
852
- for (const auto &instance : GetPlatformInstances ().GetInstances ()) {
855
+ for (const auto &instance : GetPlatformInstances ().GetSnapshot ()) {
853
856
if (instance.name .starts_with (name))
854
857
request.AddCompletion (instance.name );
855
858
}
@@ -897,7 +900,7 @@ PluginManager::GetProcessCreateCallbackForPluginName(llvm::StringRef name) {
897
900
898
901
void PluginManager::AutoCompleteProcessName (llvm::StringRef name,
899
902
CompletionRequest &request) {
900
- for (const auto &instance : GetProcessInstances ().GetInstances ()) {
903
+ for (const auto &instance : GetProcessInstances ().GetSnapshot ()) {
901
904
if (instance.name .starts_with (name))
902
905
request.AddCompletion (instance.name , instance.description );
903
906
}
@@ -935,11 +938,11 @@ bool PluginManager::UnregisterPlugin(
935
938
936
939
lldb::RegisterTypeBuilderSP
937
940
PluginManager::GetRegisterTypeBuilder (Target &target) {
938
- const auto &instances = GetRegisterTypeBuilderInstances ().GetInstances ();
939
941
// We assume that RegisterTypeBuilderClang is the only instance of this plugin
940
942
// type and is always present.
941
- assert (instances.size ());
942
- return instances[0 ].create_callback (target);
943
+ auto instance = GetRegisterTypeBuilderInstances ().GetInstanceAtIndex (0 );
944
+ assert (instance);
945
+ return instance->create_callback (target);
943
946
}
944
947
945
948
#pragma mark ScriptInterpreter
@@ -984,7 +987,7 @@ PluginManager::GetScriptInterpreterCreateCallbackAtIndex(uint32_t idx) {
984
987
lldb::ScriptInterpreterSP
985
988
PluginManager::GetScriptInterpreterForLanguage (lldb::ScriptLanguage script_lang,
986
989
Debugger &debugger) {
987
- const auto & instances = GetScriptInterpreterInstances ().GetInstances ();
990
+ const auto instances = GetScriptInterpreterInstances ().GetSnapshot ();
988
991
ScriptInterpreterCreateInstance none_instance = nullptr ;
989
992
for (const auto &instance : instances) {
990
993
if (instance.language == lldb::eScriptLanguageNone)
@@ -1046,13 +1049,12 @@ PluginManager::GetStructuredDataPluginCreateCallbackAtIndex(uint32_t idx) {
1046
1049
StructuredDataFilterLaunchInfo
1047
1050
PluginManager::GetStructuredDataFilterCallbackAtIndex (
1048
1051
uint32_t idx, bool &iteration_complete) {
1049
- const auto &instances = GetStructuredDataPluginInstances (). GetInstances ();
1050
- if (idx < instances. size ( )) {
1052
+ if ( auto instance =
1053
+ GetStructuredDataPluginInstances (). GetInstanceAtIndex (idx )) {
1051
1054
iteration_complete = false ;
1052
- return instances[idx].filter_callback ;
1053
- } else {
1054
- iteration_complete = true ;
1055
+ return instance->filter_callback ;
1055
1056
}
1057
+ iteration_complete = true ;
1056
1058
return nullptr ;
1057
1059
}
1058
1060
@@ -1167,7 +1169,7 @@ PluginManager::GetSymbolLocatorCreateCallbackAtIndex(uint32_t idx) {
1167
1169
1168
1170
ModuleSpec
1169
1171
PluginManager::LocateExecutableObjectFile (const ModuleSpec &module_spec) {
1170
- auto & instances = GetSymbolLocatorInstances ().GetInstances ();
1172
+ auto instances = GetSymbolLocatorInstances ().GetSnapshot ();
1171
1173
for (auto &instance : instances) {
1172
1174
if (instance.locate_executable_object_file ) {
1173
1175
std::optional<ModuleSpec> result =
@@ -1181,7 +1183,7 @@ PluginManager::LocateExecutableObjectFile(const ModuleSpec &module_spec) {
1181
1183
1182
1184
FileSpec PluginManager::LocateExecutableSymbolFile (
1183
1185
const ModuleSpec &module_spec, const FileSpecList &default_search_paths) {
1184
- auto & instances = GetSymbolLocatorInstances ().GetInstances ();
1186
+ auto instances = GetSymbolLocatorInstances ().GetSnapshot ();
1185
1187
for (auto &instance : instances) {
1186
1188
if (instance.locate_executable_symbol_file ) {
1187
1189
std::optional<FileSpec> result = instance.locate_executable_symbol_file (
@@ -1197,7 +1199,7 @@ bool PluginManager::DownloadObjectAndSymbolFile(ModuleSpec &module_spec,
1197
1199
Status &error,
1198
1200
bool force_lookup,
1199
1201
bool copy_executable) {
1200
- auto & instances = GetSymbolLocatorInstances ().GetInstances ();
1202
+ auto instances = GetSymbolLocatorInstances ().GetSnapshot ();
1201
1203
for (auto &instance : instances) {
1202
1204
if (instance.download_object_symbol_file ) {
1203
1205
if (instance.download_object_symbol_file (module_spec, error, force_lookup,
@@ -1211,7 +1213,7 @@ bool PluginManager::DownloadObjectAndSymbolFile(ModuleSpec &module_spec,
1211
1213
FileSpec PluginManager::FindSymbolFileInBundle (const FileSpec &symfile_bundle,
1212
1214
const UUID *uuid,
1213
1215
const ArchSpec *arch) {
1214
- auto & instances = GetSymbolLocatorInstances ().GetInstances ();
1216
+ auto instances = GetSymbolLocatorInstances ().GetSnapshot ();
1215
1217
for (auto &instance : instances) {
1216
1218
if (instance.find_symbol_file_in_bundle ) {
1217
1219
std::optional<FileSpec> result =
@@ -1272,21 +1274,20 @@ PluginManager::GetTraceCreateCallback(llvm::StringRef plugin_name) {
1272
1274
1273
1275
TraceCreateInstanceForLiveProcess
1274
1276
PluginManager::GetTraceCreateCallbackForLiveProcess (llvm::StringRef plugin_name) {
1275
- for ( const TraceInstance & instance : GetTracePluginInstances ().GetInstances ( ))
1276
- if ( instance. name == plugin_name)
1277
- return instance. create_callback_for_live_process ;
1277
+ if ( auto instance = GetTracePluginInstances ().GetInstanceForName (plugin_name ))
1278
+ return instance-> create_callback_for_live_process ;
1279
+
1278
1280
return nullptr ;
1279
1281
}
1280
1282
1281
1283
llvm::StringRef PluginManager::GetTraceSchema (llvm::StringRef plugin_name) {
1282
- for (const TraceInstance &instance : GetTracePluginInstances ().GetInstances ())
1283
- if (instance.name == plugin_name)
1284
- return instance.schema ;
1284
+ if (auto instance = GetTracePluginInstances ().GetInstanceForName (plugin_name))
1285
+ return instance->schema ;
1285
1286
return llvm::StringRef ();
1286
1287
}
1287
1288
1288
1289
llvm::StringRef PluginManager::GetTraceSchema (size_t index) {
1289
- if (TraceInstance *instance =
1290
+ if (const TraceInstance *instance =
1290
1291
GetTracePluginInstances ().GetInstanceAtIndex (index))
1291
1292
return instance->schema ;
1292
1293
return llvm::StringRef ();
@@ -1335,7 +1336,7 @@ bool PluginManager::UnregisterPlugin(
1335
1336
1336
1337
ThreadTraceExportCommandCreator
1337
1338
PluginManager::GetThreadTraceExportCommandCreatorAtIndex (uint32_t index) {
1338
- if (TraceExporterInstance *instance =
1339
+ if (const TraceExporterInstance *instance =
1339
1340
GetTraceExporterInstances ().GetInstanceAtIndex (index))
1340
1341
return instance->create_thread_trace_export_command ;
1341
1342
return nullptr ;
@@ -1438,9 +1439,9 @@ bool PluginManager::UnregisterPlugin(
1438
1439
1439
1440
InstrumentationRuntimeGetType
1440
1441
PluginManager::GetInstrumentationRuntimeGetTypeCallbackAtIndex (uint32_t idx) {
1441
- const auto &instances = GetInstrumentationRuntimeInstances (). GetInstances ();
1442
- if (idx < instances. size ( ))
1443
- return instances[idx]. get_type_callback ;
1442
+ if ( auto instance =
1443
+ GetInstrumentationRuntimeInstances (). GetInstanceAtIndex (idx ))
1444
+ return instance-> get_type_callback ;
1444
1445
return nullptr ;
1445
1446
}
1446
1447
@@ -1493,15 +1494,15 @@ PluginManager::GetTypeSystemCreateCallbackAtIndex(uint32_t idx) {
1493
1494
}
1494
1495
1495
1496
LanguageSet PluginManager::GetAllTypeSystemSupportedLanguagesForTypes () {
1496
- const auto & instances = GetTypeSystemInstances ().GetInstances ();
1497
+ const auto instances = GetTypeSystemInstances ().GetSnapshot ();
1497
1498
LanguageSet all;
1498
1499
for (unsigned i = 0 ; i < instances.size (); ++i)
1499
1500
all.bitvector |= instances[i].supported_languages_for_types .bitvector ;
1500
1501
return all;
1501
1502
}
1502
1503
1503
1504
LanguageSet PluginManager::GetAllTypeSystemSupportedLanguagesForExpressions () {
1504
- const auto & instances = GetTypeSystemInstances ().GetInstances ();
1505
+ const auto instances = GetTypeSystemInstances ().GetSnapshot ();
1505
1506
LanguageSet all;
1506
1507
for (unsigned i = 0 ; i < instances.size (); ++i)
1507
1508
all.bitvector |= instances[i].supported_languages_for_expressions .bitvector ;
@@ -1545,7 +1546,7 @@ bool PluginManager::UnregisterPlugin(
1545
1546
}
1546
1547
1547
1548
uint32_t PluginManager::GetNumScriptedInterfaces () {
1548
- return GetScriptedInterfaceInstances ().GetInstances ().size ();
1549
+ return GetScriptedInterfaceInstances ().GetSnapshot ().size ();
1549
1550
}
1550
1551
1551
1552
llvm::StringRef PluginManager::GetScriptedInterfaceNameAtIndex (uint32_t index) {
@@ -1559,17 +1560,16 @@ PluginManager::GetScriptedInterfaceDescriptionAtIndex(uint32_t index) {
1559
1560
1560
1561
lldb::ScriptLanguage
1561
1562
PluginManager::GetScriptedInterfaceLanguageAtIndex (uint32_t idx) {
1562
- const auto &instances = GetScriptedInterfaceInstances ().GetInstances ();
1563
- return idx < instances. size () ? instances[idx]. language
1564
- : ScriptLanguage::eScriptLanguageNone;
1563
+ if ( auto instance = GetScriptedInterfaceInstances ().GetInstanceAtIndex (idx))
1564
+ return instance-> language ;
1565
+ return ScriptLanguage::eScriptLanguageNone;
1565
1566
}
1566
1567
1567
1568
ScriptedInterfaceUsages
1568
1569
PluginManager::GetScriptedInterfaceUsagesAtIndex (uint32_t idx) {
1569
- const auto &instances = GetScriptedInterfaceInstances ().GetInstances ();
1570
- if (idx >= instances.size ())
1571
- return {};
1572
- return instances[idx].usages ;
1570
+ if (auto instance = GetScriptedInterfaceInstances ().GetInstanceAtIndex (idx))
1571
+ return instance->usages ;
1572
+ return {};
1573
1573
}
1574
1574
1575
1575
#pragma mark REPL
@@ -1606,13 +1606,13 @@ REPLCreateInstance PluginManager::GetREPLCreateCallbackAtIndex(uint32_t idx) {
1606
1606
}
1607
1607
1608
1608
LanguageSet PluginManager::GetREPLSupportedLanguagesAtIndex (uint32_t idx) {
1609
- const auto &instances = GetREPLInstances ().GetInstances ();
1610
- return idx < instances. size () ? instances[idx]. supported_languages
1611
- : LanguageSet ();
1609
+ if ( auto instance = GetREPLInstances ().GetInstanceAtIndex (idx))
1610
+ return instance-> supported_languages ;
1611
+ return LanguageSet ();
1612
1612
}
1613
1613
1614
1614
LanguageSet PluginManager::GetREPLAllTypeSystemSupportedLanguages () {
1615
- const auto & instances = GetREPLInstances ().GetInstances ();
1615
+ const auto instances = GetREPLInstances ().GetSnapshot ();
1616
1616
LanguageSet all;
1617
1617
for (unsigned i = 0 ; i < instances.size (); ++i)
1618
1618
all.bitvector |= instances[i].supported_languages .bitvector ;
0 commit comments