@@ -1078,16 +1078,16 @@ void SwiftASTContext::SetCompilerInvocationLLDBOverrides() {
1078
1078
}
1079
1079
1080
1080
SwiftASTContext::~SwiftASTContext () {
1081
- if (swift::ASTContext * ctx = m_ast_context_ap. get ())
1081
+ if (auto ctx = GetASTContext ())
1082
1082
// A RemoteASTContext associated with this swift::ASTContext has
1083
1083
// to be destroyed before the swift::ASTContext is destroyed.
1084
- assert (!GetASTMap ().Lookup (ctx) && " ast context still in global map" );
1084
+ assert (!GetASTMap ().Lookup (* ctx) && " ast context still in global map" );
1085
1085
}
1086
1086
1087
1087
1088
1088
SwiftASTContextForModule::~SwiftASTContextForModule () {
1089
- if (swift::ASTContext * ctx = m_ast_context_ap. get ())
1090
- GetASTMap ().Erase (ctx);
1089
+ if (auto ctx = GetASTContext ())
1090
+ GetASTMap ().Erase (* ctx);
1091
1091
}
1092
1092
1093
1093
// / This code comes from CompilerInvocation.cpp (setRuntimeResourcePath).
@@ -2579,8 +2579,9 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
2579
2579
2580
2580
// Report progress on module importing by using a callback function in
2581
2581
// swift::ASTContext
2582
+ auto ast_context = swift_ast_sp->GetASTContext ();
2582
2583
Progress progress (" Importing Swift standard library" );
2583
- swift_ast_sp-> m_ast_context_ap ->SetPreModuleImportCallback (
2584
+ ast_context ->SetPreModuleImportCallback (
2584
2585
[&progress](llvm::StringRef module_name,
2585
2586
swift::ASTContext::ModuleImportKind kind) {
2586
2587
progress.Increment (1 , module_name.str ());
@@ -2589,13 +2590,13 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
2589
2590
// Clear the callback function on scope exit to prevent an out-of-scope
2590
2591
// access of the progress local variable
2591
2592
auto on_exit = llvm::make_scope_exit ([&]() {
2592
- swift_ast_sp-> m_ast_context_ap ->SetPreModuleImportCallback (
2593
+ ast_context ->SetPreModuleImportCallback (
2593
2594
[](llvm::StringRef module_name,
2594
2595
swift::ASTContext::ModuleImportKind kind) {});
2595
2596
});
2596
2597
2597
2598
swift::ModuleDecl *stdlib =
2598
- swift_ast_sp-> m_ast_context_ap ->getStdlibModule (can_create);
2599
+ ast_context ->getStdlibModule (can_create);
2599
2600
if (!stdlib || IsDWARFImported (*stdlib)) {
2600
2601
logError (" couldn't load the Swift stdlib" );
2601
2602
return {};
@@ -3189,10 +3190,11 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
3189
3190
{
3190
3191
LLDB_SCOPED_TIMERF (" %s (getStdlibModule)" , m_description.c_str ());
3191
3192
3193
+ auto ast_context = swift_ast_sp->GetASTContext ();
3192
3194
// Report progress on module importing by using a callback function in
3193
3195
// swift::ASTContext
3194
3196
Progress progress (" Importing Swift standard library" );
3195
- swift_ast_sp-> m_ast_context_ap ->SetPreModuleImportCallback (
3197
+ ast_context ->SetPreModuleImportCallback (
3196
3198
[&progress](llvm::StringRef module_name,
3197
3199
swift::ASTContext::ModuleImportKind kind) {
3198
3200
progress.Increment (1 , module_name.str ());
@@ -3201,14 +3203,14 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
3201
3203
// Clear the callback function on scope exit to prevent an out-of-scope
3202
3204
// access of the progress local variable
3203
3205
auto on_exit = llvm::make_scope_exit ([&]() {
3204
- swift_ast_sp-> m_ast_context_ap ->SetPreModuleImportCallback (
3206
+ ast_context ->SetPreModuleImportCallback (
3205
3207
[](llvm::StringRef module_name,
3206
3208
swift::ASTContext::ModuleImportKind kind) {});
3207
3209
});
3208
3210
3209
3211
const bool can_create = true ;
3210
3212
swift::ModuleDecl *stdlib =
3211
- swift_ast_sp-> m_ast_context_ap ->getStdlibModule (can_create);
3213
+ ast_context ->getStdlibModule (can_create);
3212
3214
if (!stdlib || IsDWARFImported (*stdlib)) {
3213
3215
logError (" couldn't load the Swift stdlib" );
3214
3216
return {};
@@ -3429,10 +3431,17 @@ bool SwiftASTContext::SetTriple(const llvm::Triple triple, Module *module) {
3429
3431
3430
3432
m_compiler_invocation_ap->setTargetTriple (adjusted_triple);
3431
3433
3434
+ #ifndef NDEBUG
3432
3435
assert (GetTriple () == adjusted_triple);
3436
+ // We can't call GetASTContext() here because
3437
+ // m_initialized_search_path_options and m_initialized_clang_importer_options
3438
+ // need to be initialized before initializing the AST context.
3439
+ m_ast_context_mutex.lock ();
3433
3440
assert (!m_ast_context_ap ||
3434
3441
(llvm::Triple (m_ast_context_ap->LangOpts .Target .getTriple ()) ==
3435
3442
adjusted_triple));
3443
+ m_ast_context_mutex.unlock ();
3444
+ #endif
3436
3445
3437
3446
// Every time the triple is changed the LangOpts must be updated
3438
3447
// too, because Swift default-initializes the EnableObjCInterop
@@ -3848,6 +3857,10 @@ ThreadSafeASTContext SwiftASTContext::GetASTContext() {
3848
3857
return {m_ast_context_ap.get (), m_ast_context_mutex};
3849
3858
}
3850
3859
3860
+ ThreadSafeASTContext SwiftASTContext::GetASTContext () const {
3861
+ return const_cast <SwiftASTContext *>(this )->GetASTContext ();
3862
+ }
3863
+
3851
3864
swift::MemoryBufferSerializedModuleLoader *
3852
3865
SwiftASTContext::GetMemoryBufferModuleLoader () {
3853
3866
VALID_OR_RETURN (nullptr );
@@ -3863,14 +3876,6 @@ swift::ClangImporter *SwiftASTContext::GetClangImporter() {
3863
3876
return m_clangimporter;
3864
3877
}
3865
3878
3866
- const swift::SearchPathOptions *SwiftASTContext::GetSearchPathOptions () const {
3867
- VALID_OR_RETURN (0 );
3868
-
3869
- if (!m_ast_context_ap)
3870
- return nullptr ;
3871
- return &m_ast_context_ap->SearchPathOpts ;
3872
- }
3873
-
3874
3879
const std::vector<std::string> &SwiftASTContext::GetClangArguments () {
3875
3880
return GetClangImporterOptions ().ExtraArgs ;
3876
3881
}
@@ -4860,8 +4865,7 @@ SwiftASTContext::ReconstructType(ConstString mangled_typename) {
4860
4865
4861
4866
CompilerType SwiftASTContext::GetAnyObjectType () {
4862
4867
VALID_OR_RETURN (CompilerType ());
4863
- ThreadSafeASTContext ast = GetASTContext ();
4864
- return ToCompilerType ({ast->getAnyObjectType ()});
4868
+ return ToCompilerType ({GetASTContext ()->getAnyObjectType ()});
4865
4869
}
4866
4870
4867
4871
static CompilerType ValueDeclToType (swift::ValueDecl *decl) {
@@ -4994,7 +4998,7 @@ SwiftASTContext::FindContainedTypeOrDecl(llvm::StringRef name,
4994
4998
return 0 ;
4995
4999
swift::NominalTypeDecl *nominal_decl = nominal_type->getDecl ();
4996
5000
llvm::ArrayRef<swift::ValueDecl *> decls = nominal_decl->lookupDirect (
4997
- swift::DeclName (m_ast_context_ap ->getIdentifier (name)));
5001
+ swift::DeclName (GetASTContext () ->getIdentifier (name)));
4998
5002
for (auto *decl : decls)
4999
5003
results.emplace (DeclToTypeOrDecl (decl));
5000
5004
}
@@ -5135,7 +5139,8 @@ CompilerType SwiftASTContext::ImportType(CompilerType &type, Status &error) {
5135
5139
VALID_OR_RETURN (CompilerType ());
5136
5140
LLDB_SCOPED_TIMER ();
5137
5141
5138
- if (m_ast_context_ap.get () == NULL )
5142
+ auto ast_context = GetASTContext ();
5143
+ if (!ast_context)
5139
5144
return CompilerType ();
5140
5145
5141
5146
auto ts = type.GetTypeSystem ();
@@ -5208,7 +5213,7 @@ swift::ModuleDecl *SwiftASTContext::GetScratchModule() {
5208
5213
if (m_scratch_module == nullptr ) {
5209
5214
ThreadSafeASTContext ast_ctx = GetASTContext ();
5210
5215
m_scratch_module = swift::ModuleDecl::createEmpty (
5211
- GetASTContext () ->getIdentifier (" __lldb_scratch_module" ), **ast_ctx);
5216
+ ast_ctx ->getIdentifier (" __lldb_scratch_module" ), **ast_ctx);
5212
5217
}
5213
5218
return m_scratch_module;
5214
5219
}
@@ -5306,12 +5311,13 @@ SwiftASTContext::CreateTupleType(const std::vector<TupleElement> &elements) {
5306
5311
std::vector<swift::TupleTypeElt> tuple_elems;
5307
5312
for (const TupleElement &element : elements) {
5308
5313
if (auto swift_type = GetSwiftTypeIgnoringErrors (element.element_type )) {
5309
- if (element.element_name .IsEmpty ())
5314
+ if (element.element_name .IsEmpty ()) {
5310
5315
tuple_elems.push_back (swift::TupleTypeElt (swift_type));
5311
- else
5316
+ } else {
5312
5317
tuple_elems.push_back (swift::TupleTypeElt (
5313
- swift_type, m_ast_context_ap ->getIdentifier (
5318
+ swift_type, GetASTContext () ->getIdentifier (
5314
5319
element.element_name .GetCString ())));
5320
+ }
5315
5321
} else
5316
5322
return {};
5317
5323
}
@@ -5421,7 +5427,7 @@ void SwiftASTContext::PrintDiagnostics(DiagnosticManager &diagnostic_manager,
5421
5427
uint32_t last_line) const {
5422
5428
// VALID_OR_RETURN cannot be used here here since would exit on error.
5423
5429
LLDB_SCOPED_TIMER ();
5424
- if (!m_ast_context_ap. get ()) {
5430
+ if (!GetASTContext ()) {
5425
5431
RaiseFatalError (" Swift compiler could not be initialized" );
5426
5432
return ;
5427
5433
}
@@ -5431,7 +5437,7 @@ void SwiftASTContext::PrintDiagnostics(DiagnosticManager &diagnostic_manager,
5431
5437
assert (m_diagnostic_consumer_ap);
5432
5438
auto &diags =
5433
5439
*static_cast <StoringDiagnosticConsumer *>(m_diagnostic_consumer_ap.get ());
5434
- if (m_ast_context_ap ->Diags .hasFatalErrorOccurred () &&
5440
+ if (GetASTContext () ->Diags .hasFatalErrorOccurred () &&
5435
5441
!m_reported_fatal_error) {
5436
5442
DiagnosticManager fatal_diagnostics;
5437
5443
diags.PrintDiagnostics (fatal_diagnostics, {}, bufferID, first_line,
@@ -5518,56 +5524,57 @@ void SwiftASTContext::LogConfiguration(bool is_repl) {
5518
5524
// want the logs in the error case!
5519
5525
HEALTH_LOG_PRINTF (" (SwiftASTContext*)%p:" , static_cast <void *>(this ));
5520
5526
5521
- if (!m_ast_context_ap) {
5527
+ auto ast_context = GetASTContext ();
5528
+ if (!ast_context) {
5522
5529
HEALTH_LOG_PRINTF (" (no AST context)" );
5523
5530
return ;
5524
5531
}
5525
5532
if (is_repl)
5526
5533
HEALTH_LOG_PRINTF (" REPL : true" );
5527
5534
HEALTH_LOG_PRINTF (" Swift/C++ interop : %s" ,
5528
- m_ast_context_ap ->LangOpts .EnableCXXInterop ? " on" : " off" );
5535
+ ast_context ->LangOpts .EnableCXXInterop ? " on" : " off" );
5529
5536
HEALTH_LOG_PRINTF (" Swift/Objective-C interop : %s" ,
5530
- m_ast_context_ap ->LangOpts .EnableObjCInterop ? " on" : " off" );
5537
+ ast_context ->LangOpts .EnableObjCInterop ? " on" : " off" );
5531
5538
5532
5539
HEALTH_LOG_PRINTF (" Architecture : %s" ,
5533
- m_ast_context_ap ->LangOpts .Target .getTriple ().c_str ());
5540
+ ast_context ->LangOpts .Target .getTriple ().c_str ());
5534
5541
HEALTH_LOG_PRINTF (
5535
5542
" SDK path : %s" ,
5536
- m_ast_context_ap ->SearchPathOpts .getSDKPath ().str ().c_str ());
5543
+ ast_context ->SearchPathOpts .getSDKPath ().str ().c_str ());
5537
5544
HEALTH_LOG_PRINTF (
5538
5545
" Runtime resource path : %s" ,
5539
- m_ast_context_ap ->SearchPathOpts .RuntimeResourcePath .c_str ());
5546
+ ast_context ->SearchPathOpts .RuntimeResourcePath .c_str ());
5540
5547
HEALTH_LOG_PRINTF (" Runtime library paths : (%llu items)" ,
5541
- (unsigned long long )m_ast_context_ap ->SearchPathOpts
5548
+ (unsigned long long )ast_context ->SearchPathOpts
5542
5549
.RuntimeLibraryPaths .size ());
5543
5550
5544
5551
for (const auto &runtime_library_path :
5545
- m_ast_context_ap ->SearchPathOpts .RuntimeLibraryPaths )
5552
+ ast_context ->SearchPathOpts .RuntimeLibraryPaths )
5546
5553
HEALTH_LOG_PRINTF (" %s" , runtime_library_path.c_str ());
5547
5554
5548
5555
HEALTH_LOG_PRINTF (" Runtime library import paths : (%llu items)" ,
5549
- (unsigned long long )m_ast_context_ap ->SearchPathOpts
5556
+ (unsigned long long )ast_context ->SearchPathOpts
5550
5557
.getRuntimeLibraryImportPaths ()
5551
5558
.size ());
5552
5559
5553
5560
for (const auto &runtime_import_path :
5554
- m_ast_context_ap ->SearchPathOpts .getRuntimeLibraryImportPaths ())
5561
+ ast_context ->SearchPathOpts .getRuntimeLibraryImportPaths ())
5555
5562
HEALTH_LOG_PRINTF (" %s" , runtime_import_path.c_str ());
5556
5563
5557
5564
HEALTH_LOG_PRINTF (" Framework search paths : (%llu items)" ,
5558
- (unsigned long long )m_ast_context_ap ->SearchPathOpts
5565
+ (unsigned long long )ast_context ->SearchPathOpts
5559
5566
.getFrameworkSearchPaths ()
5560
5567
.size ());
5561
5568
for (const auto &framework_search_path :
5562
- m_ast_context_ap ->SearchPathOpts .getFrameworkSearchPaths ())
5569
+ ast_context ->SearchPathOpts .getFrameworkSearchPaths ())
5563
5570
HEALTH_LOG_PRINTF (" %s" , framework_search_path.Path .c_str ());
5564
5571
5565
5572
HEALTH_LOG_PRINTF (" Import search paths : (%llu items)" ,
5566
- (unsigned long long )m_ast_context_ap ->SearchPathOpts
5573
+ (unsigned long long )ast_context ->SearchPathOpts
5567
5574
.getImportSearchPaths ()
5568
5575
.size ());
5569
5576
for (const auto &import_search_path :
5570
- m_ast_context_ap ->SearchPathOpts .getImportSearchPaths ())
5577
+ ast_context ->SearchPathOpts .getImportSearchPaths ())
5571
5578
HEALTH_LOG_PRINTF (" %s" , import_search_path.Path .c_str ());
5572
5579
5573
5580
swift::ClangImporterOptions &clang_importer_options =
@@ -5587,9 +5594,9 @@ void SwiftASTContext::LogConfiguration(bool is_repl) {
5587
5594
HEALTH_LOG_PRINTF (" %s" , extra_arg.c_str ());
5588
5595
5589
5596
HEALTH_LOG_PRINTF (" Plugin search options : (%llu items)" ,
5590
- (unsigned long long )m_ast_context_ap ->SearchPathOpts
5597
+ (unsigned long long )ast_context ->SearchPathOpts
5591
5598
.PluginSearchOpts .size ());
5592
- for (auto &elem : m_ast_context_ap ->SearchPathOpts .PluginSearchOpts ) {
5599
+ for (auto &elem : ast_context ->SearchPathOpts .PluginSearchOpts ) {
5593
5600
if (auto *opt =
5594
5601
elem.dyn_cast <swift::PluginSearchOption::LoadPluginLibrary>()) {
5595
5602
HEALTH_LOG_PRINTF (" -load-plugin-library %s" ,
@@ -8938,21 +8945,21 @@ SwiftASTContextForExpressions::SwiftASTContextForExpressions(
8938
8945
8939
8946
SwiftASTContextForExpressions::~SwiftASTContextForExpressions () {
8940
8947
LOG_PRINTF (GetLog (LLDBLog::Types | LLDBLog::Expressions), " tearing down" );
8941
- swift::ASTContext *ctx = m_ast_context_ap. get ();
8942
- if (!ctx )
8948
+ auto swift_context = GetASTContext ();
8949
+ if (!swift_context )
8943
8950
return ;
8944
8951
// A RemoteASTContext associated with this swift::ASTContext has
8945
8952
// to be destroyed before the swift::ASTContext is destroyed.
8946
8953
if (TargetSP target_sp = GetTargetWP ().lock ()) {
8947
8954
if (ProcessSP process_sp = target_sp->GetProcessSP ())
8948
8955
if (auto *runtime = SwiftLanguageRuntime::Get (process_sp))
8949
- runtime->ReleaseAssociatedRemoteASTContext (ctx );
8956
+ runtime->ReleaseAssociatedRemoteASTContext (*swift_context );
8950
8957
} else {
8951
8958
LOG_PRINTF (GetLog (LLDBLog::Types | LLDBLog::Expressions),
8952
8959
" Failed to lock target in ~SwiftASTContextForExpressions()." );
8953
8960
}
8954
8961
8955
- GetASTMap ().Erase (ctx );
8962
+ GetASTMap ().Erase (*swift_context );
8956
8963
}
8957
8964
8958
8965
PersistentExpressionState *
0 commit comments