Skip to content

Commit 892aa72

Browse files
authored
[lldb] Use GetASTContext() instead of accessing the ivar directly (#10716)
GetASTContext() returns a thread safe object. The SwiftASTContext class would require users of the class to use the function, but internally would access the underlying ivar directly. This patch makes sure all the accesses of the swift::ASTContext are done through the aforementioned function. rdar://143542489
1 parent f5c974c commit 892aa72

File tree

2 files changed

+59
-49
lines changed

2 files changed

+59
-49
lines changed

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,16 +1079,19 @@ void SwiftASTContext::SetCompilerInvocationLLDBOverrides() {
10791079
}
10801080

10811081
SwiftASTContext::~SwiftASTContext() {
1082+
#ifndef NDEBUG
1083+
m_ast_context_mutex.lock();
10821084
if (swift::ASTContext *ctx = m_ast_context_ap.get())
10831085
// A RemoteASTContext associated with this swift::ASTContext has
10841086
// to be destroyed before the swift::ASTContext is destroyed.
10851087
assert(!GetASTMap().Lookup(ctx) && "ast context still in global map");
1088+
m_ast_context_mutex.unlock();
1089+
#endif
10861090
}
10871091

1088-
10891092
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);
10921095
}
10931096

10941097
/// This code comes from CompilerInvocation.cpp (setRuntimeResourcePath).
@@ -2581,8 +2584,9 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
25812584

25822585
// Report progress on module importing by using a callback function in
25832586
// swift::ASTContext
2587+
auto ast_context = swift_ast_sp->GetASTContext();
25842588
Progress progress("Importing Swift standard library");
2585-
swift_ast_sp->m_ast_context_ap->SetPreModuleImportCallback(
2589+
ast_context->SetPreModuleImportCallback(
25862590
[&progress](llvm::StringRef module_name,
25872591
swift::ASTContext::ModuleImportKind kind) {
25882592
progress.Increment(1, module_name.str());
@@ -2591,13 +2595,13 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
25912595
// Clear the callback function on scope exit to prevent an out-of-scope
25922596
// access of the progress local variable
25932597
auto on_exit = llvm::make_scope_exit([&]() {
2594-
swift_ast_sp->m_ast_context_ap->SetPreModuleImportCallback(
2598+
ast_context->SetPreModuleImportCallback(
25952599
[](llvm::StringRef module_name,
25962600
swift::ASTContext::ModuleImportKind kind) {});
25972601
});
25982602

25992603
swift::ModuleDecl *stdlib =
2600-
swift_ast_sp->m_ast_context_ap->getStdlibModule(can_create);
2604+
ast_context->getStdlibModule(can_create);
26012605
if (!stdlib || IsDWARFImported(*stdlib)) {
26022606
logError("couldn't load the Swift stdlib");
26032607
return {};
@@ -3149,10 +3153,11 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
31493153
}
31503154

31513155
{
3156+
auto ast_context = swift_ast_sp->GetASTContext();
31523157
// Report progress on module importing by using a callback function in
31533158
// swift::ASTContext
31543159
Progress progress("Importing Swift standard library");
3155-
swift_ast_sp->m_ast_context_ap->SetPreModuleImportCallback(
3160+
ast_context->SetPreModuleImportCallback(
31563161
[&progress](llvm::StringRef module_name,
31573162
swift::ASTContext::ModuleImportKind kind) {
31583163
progress.Increment(1, module_name.str());
@@ -3161,14 +3166,14 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
31613166
// Clear the callback function on scope exit to prevent an out-of-scope
31623167
// access of the progress local variable
31633168
auto on_exit = llvm::make_scope_exit([&]() {
3164-
swift_ast_sp->m_ast_context_ap->SetPreModuleImportCallback(
3169+
ast_context->SetPreModuleImportCallback(
31653170
[](llvm::StringRef module_name,
31663171
swift::ASTContext::ModuleImportKind kind) {});
31673172
});
31683173

31693174
const bool can_create = true;
31703175
swift::ModuleDecl *stdlib =
3171-
swift_ast_sp->m_ast_context_ap->getStdlibModule(can_create);
3176+
ast_context->getStdlibModule(can_create);
31723177
if (!stdlib || IsDWARFImported(*stdlib)) {
31733178
logError("couldn't load the Swift stdlib");
31743179
return {};
@@ -3360,10 +3365,17 @@ bool SwiftASTContext::SetTriple(const llvm::Triple triple, Module *module) {
33603365

33613366
m_compiler_invocation_ap->setTargetTriple(adjusted_triple);
33623367

3368+
#ifndef NDEBUG
33633369
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();
33643374
assert(!m_ast_context_ap ||
33653375
(llvm::Triple(m_ast_context_ap->LangOpts.Target.getTriple()) ==
33663376
adjusted_triple));
3377+
m_ast_context_mutex.unlock();
3378+
#endif
33673379

33683380
// Every time the triple is changed the LangOpts must be updated
33693381
// too, because Swift default-initializes the EnableObjCInterop
@@ -3777,6 +3789,10 @@ ThreadSafeASTContext SwiftASTContext::GetASTContext() {
37773789
return {m_ast_context_ap.get(), m_ast_context_mutex};
37783790
}
37793791

3792+
ThreadSafeASTContext SwiftASTContext::GetASTContext() const {
3793+
return const_cast<SwiftASTContext *>(this)->GetASTContext();
3794+
}
3795+
37803796
swift::MemoryBufferSerializedModuleLoader *
37813797
SwiftASTContext::GetMemoryBufferModuleLoader() {
37823798
VALID_OR_RETURN(nullptr);
@@ -3792,14 +3808,6 @@ swift::ClangImporter *SwiftASTContext::GetClangImporter() {
37923808
return m_clangimporter;
37933809
}
37943810

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-
38033811
const std::vector<std::string> &SwiftASTContext::GetClangArguments() {
38043812
return GetClangImporterOptions().ExtraArgs;
38053813
}
@@ -4782,8 +4790,7 @@ SwiftASTContext::ReconstructType(ConstString mangled_typename) {
47824790

47834791
CompilerType SwiftASTContext::GetAnyObjectType() {
47844792
VALID_OR_RETURN(CompilerType());
4785-
ThreadSafeASTContext ast = GetASTContext();
4786-
return ToCompilerType({ast->getAnyObjectType()});
4793+
return ToCompilerType({GetASTContext()->getAnyObjectType()});
47874794
}
47884795

47894796
static CompilerType ValueDeclToType(swift::ValueDecl *decl) {
@@ -4915,7 +4922,7 @@ SwiftASTContext::FindContainedTypeOrDecl(llvm::StringRef name,
49154922
return 0;
49164923
swift::NominalTypeDecl *nominal_decl = nominal_type->getDecl();
49174924
llvm::ArrayRef<swift::ValueDecl *> decls = nominal_decl->lookupDirect(
4918-
swift::DeclName(m_ast_context_ap->getIdentifier(name)));
4925+
swift::DeclName(GetASTContext()->getIdentifier(name)));
49194926
for (auto *decl : decls)
49204927
results.emplace(DeclToTypeOrDecl(decl));
49214928
}
@@ -5053,7 +5060,8 @@ size_t SwiftASTContext::FindType(const char *name,
50535060
CompilerType SwiftASTContext::ImportType(CompilerType &type, Status &error) {
50545061
VALID_OR_RETURN(CompilerType());
50555062

5056-
if (m_ast_context_ap.get() == NULL)
5063+
auto ast_context = GetASTContext();
5064+
if (!ast_context)
50575065
return CompilerType();
50585066

50595067
auto ts = type.GetTypeSystem();
@@ -5126,7 +5134,7 @@ swift::ModuleDecl *SwiftASTContext::GetScratchModule() {
51265134
if (m_scratch_module == nullptr) {
51275135
ThreadSafeASTContext ast_ctx = GetASTContext();
51285136
m_scratch_module = swift::ModuleDecl::createEmpty(
5129-
GetASTContext()->getIdentifier("__lldb_scratch_module"), **ast_ctx);
5137+
ast_ctx->getIdentifier("__lldb_scratch_module"), **ast_ctx);
51305138
}
51315139
return m_scratch_module;
51325140
}
@@ -5223,12 +5231,13 @@ SwiftASTContext::CreateTupleType(const std::vector<TupleElement> &elements) {
52235231
std::vector<swift::TupleTypeElt> tuple_elems;
52245232
for (const TupleElement &element : elements) {
52255233
if (auto swift_type = GetSwiftTypeIgnoringErrors(element.element_type)) {
5226-
if (element.element_name.IsEmpty())
5234+
if (element.element_name.IsEmpty()) {
52275235
tuple_elems.push_back(swift::TupleTypeElt(swift_type));
5228-
else
5236+
} else {
52295237
tuple_elems.push_back(swift::TupleTypeElt(
5230-
swift_type, m_ast_context_ap->getIdentifier(
5238+
swift_type, GetASTContext()->getIdentifier(
52315239
element.element_name.GetCString())));
5240+
}
52325241
} else
52335242
return {};
52345243
}
@@ -5351,7 +5360,7 @@ void SwiftASTContext::PrintDiagnostics(DiagnosticManager &diagnostic_manager,
53515360
uint32_t bufferID, uint32_t first_line,
53525361
uint32_t last_line) const {
53535362
// VALID_OR_RETURN cannot be used here here since would exit on error.
5354-
if (!m_ast_context_ap.get()) {
5363+
if (!GetASTContext()) {
53555364
RaiseFatalError("Swift compiler could not be initialized");
53565365
return;
53575366
}
@@ -5361,7 +5370,7 @@ void SwiftASTContext::PrintDiagnostics(DiagnosticManager &diagnostic_manager,
53615370
assert(m_diagnostic_consumer_ap);
53625371
auto &diags =
53635372
*static_cast<StoringDiagnosticConsumer *>(m_diagnostic_consumer_ap.get());
5364-
if (m_ast_context_ap->Diags.hasFatalErrorOccurred() &&
5373+
if (GetASTContext()->Diags.hasFatalErrorOccurred() &&
53655374
!m_reported_fatal_error) {
53665375
DiagnosticManager fatal_diagnostics;
53675376
diags.PrintDiagnostics(fatal_diagnostics, {}, bufferID, first_line,
@@ -5448,56 +5457,57 @@ void SwiftASTContext::LogConfiguration(bool is_repl) {
54485457
// want the logs in the error case!
54495458
HEALTH_LOG_PRINTF("(SwiftASTContext*)%p:", static_cast<void *>(this));
54505459

5451-
if (!m_ast_context_ap) {
5460+
auto ast_context = GetASTContext();
5461+
if (!ast_context) {
54525462
HEALTH_LOG_PRINTF(" (no AST context)");
54535463
return;
54545464
}
54555465
if (is_repl)
54565466
HEALTH_LOG_PRINTF(" REPL : true");
54575467
HEALTH_LOG_PRINTF(" Swift/C++ interop : %s",
5458-
m_ast_context_ap->LangOpts.EnableCXXInterop ? "on" : "off");
5468+
ast_context->LangOpts.EnableCXXInterop ? "on" : "off");
54595469
HEALTH_LOG_PRINTF(" Swift/Objective-C interop : %s",
5460-
m_ast_context_ap->LangOpts.EnableObjCInterop ? "on" : "off");
5470+
ast_context->LangOpts.EnableObjCInterop ? "on" : "off");
54615471

54625472
HEALTH_LOG_PRINTF(" Architecture : %s",
5463-
m_ast_context_ap->LangOpts.Target.getTriple().c_str());
5473+
ast_context->LangOpts.Target.getTriple().c_str());
54645474
HEALTH_LOG_PRINTF(
54655475
" SDK path : %s",
5466-
m_ast_context_ap->SearchPathOpts.getSDKPath().str().c_str());
5476+
ast_context->SearchPathOpts.getSDKPath().str().c_str());
54675477
HEALTH_LOG_PRINTF(
54685478
" Runtime resource path : %s",
5469-
m_ast_context_ap->SearchPathOpts.RuntimeResourcePath.c_str());
5479+
ast_context->SearchPathOpts.RuntimeResourcePath.c_str());
54705480
HEALTH_LOG_PRINTF(" Runtime library paths : (%llu items)",
5471-
(unsigned long long)m_ast_context_ap->SearchPathOpts
5481+
(unsigned long long)ast_context->SearchPathOpts
54725482
.RuntimeLibraryPaths.size());
54735483

54745484
for (const auto &runtime_library_path :
5475-
m_ast_context_ap->SearchPathOpts.RuntimeLibraryPaths)
5485+
ast_context->SearchPathOpts.RuntimeLibraryPaths)
54765486
HEALTH_LOG_PRINTF(" %s", runtime_library_path.c_str());
54775487

54785488
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
54805490
.getRuntimeLibraryImportPaths()
54815491
.size());
54825492

54835493
for (const auto &runtime_import_path :
5484-
m_ast_context_ap->SearchPathOpts.getRuntimeLibraryImportPaths())
5494+
ast_context->SearchPathOpts.getRuntimeLibraryImportPaths())
54855495
HEALTH_LOG_PRINTF(" %s", runtime_import_path.c_str());
54865496

54875497
HEALTH_LOG_PRINTF(" Framework search paths : (%llu items)",
5488-
(unsigned long long)m_ast_context_ap->SearchPathOpts
5498+
(unsigned long long)ast_context->SearchPathOpts
54895499
.getFrameworkSearchPaths()
54905500
.size());
54915501
for (const auto &framework_search_path :
5492-
m_ast_context_ap->SearchPathOpts.getFrameworkSearchPaths())
5502+
ast_context->SearchPathOpts.getFrameworkSearchPaths())
54935503
HEALTH_LOG_PRINTF(" %s", framework_search_path.Path.c_str());
54945504

54955505
HEALTH_LOG_PRINTF(" Import search paths : (%llu items)",
5496-
(unsigned long long)m_ast_context_ap->SearchPathOpts
5506+
(unsigned long long)ast_context->SearchPathOpts
54975507
.getImportSearchPaths()
54985508
.size());
54995509
for (const auto &import_search_path :
5500-
m_ast_context_ap->SearchPathOpts.getImportSearchPaths())
5510+
ast_context->SearchPathOpts.getImportSearchPaths())
55015511
HEALTH_LOG_PRINTF(" %s", import_search_path.Path.c_str());
55025512

55035513
swift::ClangImporterOptions &clang_importer_options =
@@ -5517,9 +5527,9 @@ void SwiftASTContext::LogConfiguration(bool is_repl) {
55175527
HEALTH_LOG_PRINTF(" %s", extra_arg.c_str());
55185528

55195529
HEALTH_LOG_PRINTF(" Plugin search options : (%llu items)",
5520-
(unsigned long long)m_ast_context_ap->SearchPathOpts
5530+
(unsigned long long)ast_context->SearchPathOpts
55215531
.PluginSearchOpts.size());
5522-
for (auto &elem : m_ast_context_ap->SearchPathOpts.PluginSearchOpts) {
5532+
for (auto &elem : ast_context->SearchPathOpts.PluginSearchOpts) {
55235533
if (auto *opt =
55245534
elem.dyn_cast<swift::PluginSearchOption::LoadPluginLibrary>()) {
55255535
HEALTH_LOG_PRINTF(" -load-plugin-library %s",
@@ -8850,21 +8860,21 @@ SwiftASTContextForExpressions::SwiftASTContextForExpressions(
88508860

88518861
SwiftASTContextForExpressions::~SwiftASTContextForExpressions() {
88528862
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)
88558865
return;
88568866
// A RemoteASTContext associated with this swift::ASTContext has
88578867
// to be destroyed before the swift::ASTContext is destroyed.
88588868
if (TargetSP target_sp = GetTargetWP().lock()) {
88598869
if (ProcessSP process_sp = target_sp->GetProcessSP())
88608870
if (auto *runtime = SwiftLanguageRuntime::Get(process_sp))
8861-
runtime->ReleaseAssociatedRemoteASTContext(ctx);
8871+
runtime->ReleaseAssociatedRemoteASTContext(*swift_context);
88628872
} else {
88638873
LOG_PRINTF(GetLog(LLDBLog::Types | LLDBLog::Expressions),
88648874
"Failed to lock target in ~SwiftASTContextForExpressions().");
88658875
}
88668876

8867-
GetASTMap().Erase(ctx);
8877+
GetASTMap().Erase(*swift_context);
88688878
}
88698879

88708880
PersistentExpressionState *

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ class SwiftASTContext : public TypeSystemSwift {
264264

265265
ThreadSafeASTContext GetASTContext();
266266

267+
ThreadSafeASTContext GetASTContext() const;
268+
267269
swift::IRGenDebugInfoLevel GetGenerateDebugInfo();
268270

269271
static swift::PrintOptions
@@ -306,8 +308,6 @@ class SwiftASTContext : public TypeSystemSwift {
306308
m_platform_sdk_path = path.str();
307309
}
308310

309-
const swift::SearchPathOptions *GetSearchPathOptions() const;
310-
311311
/// \return the ExtraArgs of the ClangImporterOptions.
312312
const std::vector<std::string> &GetClangArguments();
313313

0 commit comments

Comments
 (0)