@@ -1079,16 +1079,19 @@ void SwiftASTContext::SetCompilerInvocationLLDBOverrides() {
1079
1079
}
1080
1080
1081
1081
SwiftASTContext::~SwiftASTContext () {
1082
+ #ifndef NDEBUG
1083
+ m_ast_context_mutex.lock ();
1082
1084
if (swift::ASTContext *ctx = m_ast_context_ap.get ())
1083
1085
// A RemoteASTContext associated with this swift::ASTContext has
1084
1086
// to be destroyed before the swift::ASTContext is destroyed.
1085
1087
assert (!GetASTMap ().Lookup (ctx) && " ast context still in global map" );
1088
+ m_ast_context_mutex.unlock ();
1089
+ #endif
1086
1090
}
1087
1091
1088
-
1089
1092
SwiftASTContextForModule::~SwiftASTContextForModule () {
1090
- if (swift::ASTContext * ctx = m_ast_context_ap. get ())
1091
- GetASTMap ().Erase (ctx);
1093
+ if (auto ctx = GetASTContext ())
1094
+ GetASTMap ().Erase (* ctx);
1092
1095
}
1093
1096
1094
1097
// / This code comes from CompilerInvocation.cpp (setRuntimeResourcePath).
@@ -2581,8 +2584,9 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
2581
2584
2582
2585
// Report progress on module importing by using a callback function in
2583
2586
// swift::ASTContext
2587
+ auto ast_context = swift_ast_sp->GetASTContext ();
2584
2588
Progress progress (" Importing Swift standard library" );
2585
- swift_ast_sp-> m_ast_context_ap ->SetPreModuleImportCallback (
2589
+ ast_context ->SetPreModuleImportCallback (
2586
2590
[&progress](llvm::StringRef module_name,
2587
2591
swift::ASTContext::ModuleImportKind kind) {
2588
2592
progress.Increment (1 , module_name.str ());
@@ -2591,13 +2595,13 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
2591
2595
// Clear the callback function on scope exit to prevent an out-of-scope
2592
2596
// access of the progress local variable
2593
2597
auto on_exit = llvm::make_scope_exit ([&]() {
2594
- swift_ast_sp-> m_ast_context_ap ->SetPreModuleImportCallback (
2598
+ ast_context ->SetPreModuleImportCallback (
2595
2599
[](llvm::StringRef module_name,
2596
2600
swift::ASTContext::ModuleImportKind kind) {});
2597
2601
});
2598
2602
2599
2603
swift::ModuleDecl *stdlib =
2600
- swift_ast_sp-> m_ast_context_ap ->getStdlibModule (can_create);
2604
+ ast_context ->getStdlibModule (can_create);
2601
2605
if (!stdlib || IsDWARFImported (*stdlib)) {
2602
2606
logError (" couldn't load the Swift stdlib" );
2603
2607
return {};
@@ -3149,10 +3153,11 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
3149
3153
}
3150
3154
3151
3155
{
3156
+ auto ast_context = swift_ast_sp->GetASTContext ();
3152
3157
// Report progress on module importing by using a callback function in
3153
3158
// swift::ASTContext
3154
3159
Progress progress (" Importing Swift standard library" );
3155
- swift_ast_sp-> m_ast_context_ap ->SetPreModuleImportCallback (
3160
+ ast_context ->SetPreModuleImportCallback (
3156
3161
[&progress](llvm::StringRef module_name,
3157
3162
swift::ASTContext::ModuleImportKind kind) {
3158
3163
progress.Increment (1 , module_name.str ());
@@ -3161,14 +3166,14 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
3161
3166
// Clear the callback function on scope exit to prevent an out-of-scope
3162
3167
// access of the progress local variable
3163
3168
auto on_exit = llvm::make_scope_exit ([&]() {
3164
- swift_ast_sp-> m_ast_context_ap ->SetPreModuleImportCallback (
3169
+ ast_context ->SetPreModuleImportCallback (
3165
3170
[](llvm::StringRef module_name,
3166
3171
swift::ASTContext::ModuleImportKind kind) {});
3167
3172
});
3168
3173
3169
3174
const bool can_create = true ;
3170
3175
swift::ModuleDecl *stdlib =
3171
- swift_ast_sp-> m_ast_context_ap ->getStdlibModule (can_create);
3176
+ ast_context ->getStdlibModule (can_create);
3172
3177
if (!stdlib || IsDWARFImported (*stdlib)) {
3173
3178
logError (" couldn't load the Swift stdlib" );
3174
3179
return {};
@@ -3360,10 +3365,17 @@ bool SwiftASTContext::SetTriple(const llvm::Triple triple, Module *module) {
3360
3365
3361
3366
m_compiler_invocation_ap->setTargetTriple (adjusted_triple);
3362
3367
3368
+ #ifndef NDEBUG
3363
3369
assert (GetTriple () == adjusted_triple);
3370
+ // We can't call GetASTContext() here because
3371
+ // m_initialized_search_path_options and m_initialized_clang_importer_options
3372
+ // need to be initialized before initializing the AST context.
3373
+ m_ast_context_mutex.lock ();
3364
3374
assert (!m_ast_context_ap ||
3365
3375
(llvm::Triple (m_ast_context_ap->LangOpts .Target .getTriple ()) ==
3366
3376
adjusted_triple));
3377
+ m_ast_context_mutex.unlock ();
3378
+ #endif
3367
3379
3368
3380
// Every time the triple is changed the LangOpts must be updated
3369
3381
// too, because Swift default-initializes the EnableObjCInterop
@@ -3777,6 +3789,10 @@ ThreadSafeASTContext SwiftASTContext::GetASTContext() {
3777
3789
return {m_ast_context_ap.get (), m_ast_context_mutex};
3778
3790
}
3779
3791
3792
+ ThreadSafeASTContext SwiftASTContext::GetASTContext () const {
3793
+ return const_cast <SwiftASTContext *>(this )->GetASTContext ();
3794
+ }
3795
+
3780
3796
swift::MemoryBufferSerializedModuleLoader *
3781
3797
SwiftASTContext::GetMemoryBufferModuleLoader () {
3782
3798
VALID_OR_RETURN (nullptr );
@@ -3792,14 +3808,6 @@ swift::ClangImporter *SwiftASTContext::GetClangImporter() {
3792
3808
return m_clangimporter;
3793
3809
}
3794
3810
3795
- const swift::SearchPathOptions *SwiftASTContext::GetSearchPathOptions () const {
3796
- VALID_OR_RETURN (0 );
3797
-
3798
- if (!m_ast_context_ap)
3799
- return nullptr ;
3800
- return &m_ast_context_ap->SearchPathOpts ;
3801
- }
3802
-
3803
3811
const std::vector<std::string> &SwiftASTContext::GetClangArguments () {
3804
3812
return GetClangImporterOptions ().ExtraArgs ;
3805
3813
}
@@ -4782,8 +4790,7 @@ SwiftASTContext::ReconstructType(ConstString mangled_typename) {
4782
4790
4783
4791
CompilerType SwiftASTContext::GetAnyObjectType () {
4784
4792
VALID_OR_RETURN (CompilerType ());
4785
- ThreadSafeASTContext ast = GetASTContext ();
4786
- return ToCompilerType ({ast->getAnyObjectType ()});
4793
+ return ToCompilerType ({GetASTContext ()->getAnyObjectType ()});
4787
4794
}
4788
4795
4789
4796
static CompilerType ValueDeclToType (swift::ValueDecl *decl) {
@@ -4915,7 +4922,7 @@ SwiftASTContext::FindContainedTypeOrDecl(llvm::StringRef name,
4915
4922
return 0 ;
4916
4923
swift::NominalTypeDecl *nominal_decl = nominal_type->getDecl ();
4917
4924
llvm::ArrayRef<swift::ValueDecl *> decls = nominal_decl->lookupDirect (
4918
- swift::DeclName (m_ast_context_ap ->getIdentifier (name)));
4925
+ swift::DeclName (GetASTContext () ->getIdentifier (name)));
4919
4926
for (auto *decl : decls)
4920
4927
results.emplace (DeclToTypeOrDecl (decl));
4921
4928
}
@@ -5053,7 +5060,8 @@ size_t SwiftASTContext::FindType(const char *name,
5053
5060
CompilerType SwiftASTContext::ImportType (CompilerType &type, Status &error) {
5054
5061
VALID_OR_RETURN (CompilerType ());
5055
5062
5056
- if (m_ast_context_ap.get () == NULL )
5063
+ auto ast_context = GetASTContext ();
5064
+ if (!ast_context)
5057
5065
return CompilerType ();
5058
5066
5059
5067
auto ts = type.GetTypeSystem ();
@@ -5126,7 +5134,7 @@ swift::ModuleDecl *SwiftASTContext::GetScratchModule() {
5126
5134
if (m_scratch_module == nullptr ) {
5127
5135
ThreadSafeASTContext ast_ctx = GetASTContext ();
5128
5136
m_scratch_module = swift::ModuleDecl::createEmpty (
5129
- GetASTContext () ->getIdentifier (" __lldb_scratch_module" ), **ast_ctx);
5137
+ ast_ctx ->getIdentifier (" __lldb_scratch_module" ), **ast_ctx);
5130
5138
}
5131
5139
return m_scratch_module;
5132
5140
}
@@ -5223,12 +5231,13 @@ SwiftASTContext::CreateTupleType(const std::vector<TupleElement> &elements) {
5223
5231
std::vector<swift::TupleTypeElt> tuple_elems;
5224
5232
for (const TupleElement &element : elements) {
5225
5233
if (auto swift_type = GetSwiftTypeIgnoringErrors (element.element_type )) {
5226
- if (element.element_name .IsEmpty ())
5234
+ if (element.element_name .IsEmpty ()) {
5227
5235
tuple_elems.push_back (swift::TupleTypeElt (swift_type));
5228
- else
5236
+ } else {
5229
5237
tuple_elems.push_back (swift::TupleTypeElt (
5230
- swift_type, m_ast_context_ap ->getIdentifier (
5238
+ swift_type, GetASTContext () ->getIdentifier (
5231
5239
element.element_name .GetCString ())));
5240
+ }
5232
5241
} else
5233
5242
return {};
5234
5243
}
@@ -5351,7 +5360,7 @@ void SwiftASTContext::PrintDiagnostics(DiagnosticManager &diagnostic_manager,
5351
5360
uint32_t bufferID, uint32_t first_line,
5352
5361
uint32_t last_line) const {
5353
5362
// VALID_OR_RETURN cannot be used here here since would exit on error.
5354
- if (!m_ast_context_ap. get ()) {
5363
+ if (!GetASTContext ()) {
5355
5364
RaiseFatalError (" Swift compiler could not be initialized" );
5356
5365
return ;
5357
5366
}
@@ -5361,7 +5370,7 @@ void SwiftASTContext::PrintDiagnostics(DiagnosticManager &diagnostic_manager,
5361
5370
assert (m_diagnostic_consumer_ap);
5362
5371
auto &diags =
5363
5372
*static_cast <StoringDiagnosticConsumer *>(m_diagnostic_consumer_ap.get ());
5364
- if (m_ast_context_ap ->Diags .hasFatalErrorOccurred () &&
5373
+ if (GetASTContext () ->Diags .hasFatalErrorOccurred () &&
5365
5374
!m_reported_fatal_error) {
5366
5375
DiagnosticManager fatal_diagnostics;
5367
5376
diags.PrintDiagnostics (fatal_diagnostics, {}, bufferID, first_line,
@@ -5448,56 +5457,57 @@ void SwiftASTContext::LogConfiguration(bool is_repl) {
5448
5457
// want the logs in the error case!
5449
5458
HEALTH_LOG_PRINTF (" (SwiftASTContext*)%p:" , static_cast <void *>(this ));
5450
5459
5451
- if (!m_ast_context_ap) {
5460
+ auto ast_context = GetASTContext ();
5461
+ if (!ast_context) {
5452
5462
HEALTH_LOG_PRINTF (" (no AST context)" );
5453
5463
return ;
5454
5464
}
5455
5465
if (is_repl)
5456
5466
HEALTH_LOG_PRINTF (" REPL : true" );
5457
5467
HEALTH_LOG_PRINTF (" Swift/C++ interop : %s" ,
5458
- m_ast_context_ap ->LangOpts .EnableCXXInterop ? " on" : " off" );
5468
+ ast_context ->LangOpts .EnableCXXInterop ? " on" : " off" );
5459
5469
HEALTH_LOG_PRINTF (" Swift/Objective-C interop : %s" ,
5460
- m_ast_context_ap ->LangOpts .EnableObjCInterop ? " on" : " off" );
5470
+ ast_context ->LangOpts .EnableObjCInterop ? " on" : " off" );
5461
5471
5462
5472
HEALTH_LOG_PRINTF (" Architecture : %s" ,
5463
- m_ast_context_ap ->LangOpts .Target .getTriple ().c_str ());
5473
+ ast_context ->LangOpts .Target .getTriple ().c_str ());
5464
5474
HEALTH_LOG_PRINTF (
5465
5475
" SDK path : %s" ,
5466
- m_ast_context_ap ->SearchPathOpts .getSDKPath ().str ().c_str ());
5476
+ ast_context ->SearchPathOpts .getSDKPath ().str ().c_str ());
5467
5477
HEALTH_LOG_PRINTF (
5468
5478
" Runtime resource path : %s" ,
5469
- m_ast_context_ap ->SearchPathOpts .RuntimeResourcePath .c_str ());
5479
+ ast_context ->SearchPathOpts .RuntimeResourcePath .c_str ());
5470
5480
HEALTH_LOG_PRINTF (" Runtime library paths : (%llu items)" ,
5471
- (unsigned long long )m_ast_context_ap ->SearchPathOpts
5481
+ (unsigned long long )ast_context ->SearchPathOpts
5472
5482
.RuntimeLibraryPaths .size ());
5473
5483
5474
5484
for (const auto &runtime_library_path :
5475
- m_ast_context_ap ->SearchPathOpts .RuntimeLibraryPaths )
5485
+ ast_context ->SearchPathOpts .RuntimeLibraryPaths )
5476
5486
HEALTH_LOG_PRINTF (" %s" , runtime_library_path.c_str ());
5477
5487
5478
5488
HEALTH_LOG_PRINTF (" Runtime library import paths : (%llu items)" ,
5479
- (unsigned long long )m_ast_context_ap ->SearchPathOpts
5489
+ (unsigned long long )ast_context ->SearchPathOpts
5480
5490
.getRuntimeLibraryImportPaths ()
5481
5491
.size ());
5482
5492
5483
5493
for (const auto &runtime_import_path :
5484
- m_ast_context_ap ->SearchPathOpts .getRuntimeLibraryImportPaths ())
5494
+ ast_context ->SearchPathOpts .getRuntimeLibraryImportPaths ())
5485
5495
HEALTH_LOG_PRINTF (" %s" , runtime_import_path.c_str ());
5486
5496
5487
5497
HEALTH_LOG_PRINTF (" Framework search paths : (%llu items)" ,
5488
- (unsigned long long )m_ast_context_ap ->SearchPathOpts
5498
+ (unsigned long long )ast_context ->SearchPathOpts
5489
5499
.getFrameworkSearchPaths ()
5490
5500
.size ());
5491
5501
for (const auto &framework_search_path :
5492
- m_ast_context_ap ->SearchPathOpts .getFrameworkSearchPaths ())
5502
+ ast_context ->SearchPathOpts .getFrameworkSearchPaths ())
5493
5503
HEALTH_LOG_PRINTF (" %s" , framework_search_path.Path .c_str ());
5494
5504
5495
5505
HEALTH_LOG_PRINTF (" Import search paths : (%llu items)" ,
5496
- (unsigned long long )m_ast_context_ap ->SearchPathOpts
5506
+ (unsigned long long )ast_context ->SearchPathOpts
5497
5507
.getImportSearchPaths ()
5498
5508
.size ());
5499
5509
for (const auto &import_search_path :
5500
- m_ast_context_ap ->SearchPathOpts .getImportSearchPaths ())
5510
+ ast_context ->SearchPathOpts .getImportSearchPaths ())
5501
5511
HEALTH_LOG_PRINTF (" %s" , import_search_path.Path .c_str ());
5502
5512
5503
5513
swift::ClangImporterOptions &clang_importer_options =
@@ -5517,9 +5527,9 @@ void SwiftASTContext::LogConfiguration(bool is_repl) {
5517
5527
HEALTH_LOG_PRINTF (" %s" , extra_arg.c_str ());
5518
5528
5519
5529
HEALTH_LOG_PRINTF (" Plugin search options : (%llu items)" ,
5520
- (unsigned long long )m_ast_context_ap ->SearchPathOpts
5530
+ (unsigned long long )ast_context ->SearchPathOpts
5521
5531
.PluginSearchOpts .size ());
5522
- for (auto &elem : m_ast_context_ap ->SearchPathOpts .PluginSearchOpts ) {
5532
+ for (auto &elem : ast_context ->SearchPathOpts .PluginSearchOpts ) {
5523
5533
if (auto *opt =
5524
5534
elem.dyn_cast <swift::PluginSearchOption::LoadPluginLibrary>()) {
5525
5535
HEALTH_LOG_PRINTF (" -load-plugin-library %s" ,
@@ -8850,21 +8860,21 @@ SwiftASTContextForExpressions::SwiftASTContextForExpressions(
8850
8860
8851
8861
SwiftASTContextForExpressions::~SwiftASTContextForExpressions () {
8852
8862
LOG_PRINTF (GetLog (LLDBLog::Types | LLDBLog::Expressions), " tearing down" );
8853
- swift::ASTContext *ctx = m_ast_context_ap. get ();
8854
- if (!ctx )
8863
+ auto swift_context = GetASTContext ();
8864
+ if (!swift_context )
8855
8865
return ;
8856
8866
// A RemoteASTContext associated with this swift::ASTContext has
8857
8867
// to be destroyed before the swift::ASTContext is destroyed.
8858
8868
if (TargetSP target_sp = GetTargetWP ().lock ()) {
8859
8869
if (ProcessSP process_sp = target_sp->GetProcessSP ())
8860
8870
if (auto *runtime = SwiftLanguageRuntime::Get (process_sp))
8861
- runtime->ReleaseAssociatedRemoteASTContext (ctx );
8871
+ runtime->ReleaseAssociatedRemoteASTContext (*swift_context );
8862
8872
} else {
8863
8873
LOG_PRINTF (GetLog (LLDBLog::Types | LLDBLog::Expressions),
8864
8874
" Failed to lock target in ~SwiftASTContextForExpressions()." );
8865
8875
}
8866
8876
8867
- GetASTMap ().Erase (ctx );
8877
+ GetASTMap ().Erase (*swift_context );
8868
8878
}
8869
8879
8870
8880
PersistentExpressionState *
0 commit comments