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