Skip to content

Commit c1ecfca

Browse files
committed
[lldb] Use GetASTContext() instead of accessing the ivar directly
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 82a5647 commit c1ecfca

File tree

2 files changed

+57
-50
lines changed

2 files changed

+57
-50
lines changed

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

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,16 +1078,16 @@ void SwiftASTContext::SetCompilerInvocationLLDBOverrides() {
10781078
}
10791079

10801080
SwiftASTContext::~SwiftASTContext() {
1081-
if (swift::ASTContext *ctx = m_ast_context_ap.get())
1081+
if (auto ctx = GetASTContext())
10821082
// A RemoteASTContext associated with this swift::ASTContext has
10831083
// 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");
10851085
}
10861086

10871087

10881088
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);
10911091
}
10921092

10931093
/// This code comes from CompilerInvocation.cpp (setRuntimeResourcePath).
@@ -2579,8 +2579,9 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
25792579

25802580
// Report progress on module importing by using a callback function in
25812581
// swift::ASTContext
2582+
auto ast_context = swift_ast_sp->GetASTContext();
25822583
Progress progress("Importing Swift standard library");
2583-
swift_ast_sp->m_ast_context_ap->SetPreModuleImportCallback(
2584+
ast_context->SetPreModuleImportCallback(
25842585
[&progress](llvm::StringRef module_name,
25852586
swift::ASTContext::ModuleImportKind kind) {
25862587
progress.Increment(1, module_name.str());
@@ -2589,13 +2590,13 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
25892590
// Clear the callback function on scope exit to prevent an out-of-scope
25902591
// access of the progress local variable
25912592
auto on_exit = llvm::make_scope_exit([&]() {
2592-
swift_ast_sp->m_ast_context_ap->SetPreModuleImportCallback(
2593+
ast_context->SetPreModuleImportCallback(
25932594
[](llvm::StringRef module_name,
25942595
swift::ASTContext::ModuleImportKind kind) {});
25952596
});
25962597

25972598
swift::ModuleDecl *stdlib =
2598-
swift_ast_sp->m_ast_context_ap->getStdlibModule(can_create);
2599+
ast_context->getStdlibModule(can_create);
25992600
if (!stdlib || IsDWARFImported(*stdlib)) {
26002601
logError("couldn't load the Swift stdlib");
26012602
return {};
@@ -3189,10 +3190,11 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
31893190
{
31903191
LLDB_SCOPED_TIMERF("%s (getStdlibModule)", m_description.c_str());
31913192

3193+
auto ast_context = swift_ast_sp->GetASTContext();
31923194
// Report progress on module importing by using a callback function in
31933195
// swift::ASTContext
31943196
Progress progress("Importing Swift standard library");
3195-
swift_ast_sp->m_ast_context_ap->SetPreModuleImportCallback(
3197+
ast_context->SetPreModuleImportCallback(
31963198
[&progress](llvm::StringRef module_name,
31973199
swift::ASTContext::ModuleImportKind kind) {
31983200
progress.Increment(1, module_name.str());
@@ -3201,14 +3203,14 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
32013203
// Clear the callback function on scope exit to prevent an out-of-scope
32023204
// access of the progress local variable
32033205
auto on_exit = llvm::make_scope_exit([&]() {
3204-
swift_ast_sp->m_ast_context_ap->SetPreModuleImportCallback(
3206+
ast_context->SetPreModuleImportCallback(
32053207
[](llvm::StringRef module_name,
32063208
swift::ASTContext::ModuleImportKind kind) {});
32073209
});
32083210

32093211
const bool can_create = true;
32103212
swift::ModuleDecl *stdlib =
3211-
swift_ast_sp->m_ast_context_ap->getStdlibModule(can_create);
3213+
ast_context->getStdlibModule(can_create);
32123214
if (!stdlib || IsDWARFImported(*stdlib)) {
32133215
logError("couldn't load the Swift stdlib");
32143216
return {};
@@ -3429,10 +3431,17 @@ bool SwiftASTContext::SetTriple(const llvm::Triple triple, Module *module) {
34293431

34303432
m_compiler_invocation_ap->setTargetTriple(adjusted_triple);
34313433

3434+
#ifndef NDEBUG
34323435
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();
34333440
assert(!m_ast_context_ap ||
34343441
(llvm::Triple(m_ast_context_ap->LangOpts.Target.getTriple()) ==
34353442
adjusted_triple));
3443+
m_ast_context_mutex.unlock();
3444+
#endif
34363445

34373446
// Every time the triple is changed the LangOpts must be updated
34383447
// too, because Swift default-initializes the EnableObjCInterop
@@ -3848,6 +3857,10 @@ ThreadSafeASTContext SwiftASTContext::GetASTContext() {
38483857
return {m_ast_context_ap.get(), m_ast_context_mutex};
38493858
}
38503859

3860+
ThreadSafeASTContext SwiftASTContext::GetASTContext() const {
3861+
return const_cast<SwiftASTContext *>(this)->GetASTContext();
3862+
}
3863+
38513864
swift::MemoryBufferSerializedModuleLoader *
38523865
SwiftASTContext::GetMemoryBufferModuleLoader() {
38533866
VALID_OR_RETURN(nullptr);
@@ -3863,14 +3876,6 @@ swift::ClangImporter *SwiftASTContext::GetClangImporter() {
38633876
return m_clangimporter;
38643877
}
38653878

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-
38743879
const std::vector<std::string> &SwiftASTContext::GetClangArguments() {
38753880
return GetClangImporterOptions().ExtraArgs;
38763881
}
@@ -4860,8 +4865,7 @@ SwiftASTContext::ReconstructType(ConstString mangled_typename) {
48604865

48614866
CompilerType SwiftASTContext::GetAnyObjectType() {
48624867
VALID_OR_RETURN(CompilerType());
4863-
ThreadSafeASTContext ast = GetASTContext();
4864-
return ToCompilerType({ast->getAnyObjectType()});
4868+
return ToCompilerType({GetASTContext()->getAnyObjectType()});
48654869
}
48664870

48674871
static CompilerType ValueDeclToType(swift::ValueDecl *decl) {
@@ -4994,7 +4998,7 @@ SwiftASTContext::FindContainedTypeOrDecl(llvm::StringRef name,
49944998
return 0;
49954999
swift::NominalTypeDecl *nominal_decl = nominal_type->getDecl();
49965000
llvm::ArrayRef<swift::ValueDecl *> decls = nominal_decl->lookupDirect(
4997-
swift::DeclName(m_ast_context_ap->getIdentifier(name)));
5001+
swift::DeclName(GetASTContext()->getIdentifier(name)));
49985002
for (auto *decl : decls)
49995003
results.emplace(DeclToTypeOrDecl(decl));
50005004
}
@@ -5135,7 +5139,8 @@ CompilerType SwiftASTContext::ImportType(CompilerType &type, Status &error) {
51355139
VALID_OR_RETURN(CompilerType());
51365140
LLDB_SCOPED_TIMER();
51375141

5138-
if (m_ast_context_ap.get() == NULL)
5142+
auto ast_context = GetASTContext();
5143+
if (!ast_context)
51395144
return CompilerType();
51405145

51415146
auto ts = type.GetTypeSystem();
@@ -5208,7 +5213,7 @@ swift::ModuleDecl *SwiftASTContext::GetScratchModule() {
52085213
if (m_scratch_module == nullptr) {
52095214
ThreadSafeASTContext ast_ctx = GetASTContext();
52105215
m_scratch_module = swift::ModuleDecl::createEmpty(
5211-
GetASTContext()->getIdentifier("__lldb_scratch_module"), **ast_ctx);
5216+
ast_ctx->getIdentifier("__lldb_scratch_module"), **ast_ctx);
52125217
}
52135218
return m_scratch_module;
52145219
}
@@ -5306,12 +5311,13 @@ SwiftASTContext::CreateTupleType(const std::vector<TupleElement> &elements) {
53065311
std::vector<swift::TupleTypeElt> tuple_elems;
53075312
for (const TupleElement &element : elements) {
53085313
if (auto swift_type = GetSwiftTypeIgnoringErrors(element.element_type)) {
5309-
if (element.element_name.IsEmpty())
5314+
if (element.element_name.IsEmpty()) {
53105315
tuple_elems.push_back(swift::TupleTypeElt(swift_type));
5311-
else
5316+
} else {
53125317
tuple_elems.push_back(swift::TupleTypeElt(
5313-
swift_type, m_ast_context_ap->getIdentifier(
5318+
swift_type, GetASTContext()->getIdentifier(
53145319
element.element_name.GetCString())));
5320+
}
53155321
} else
53165322
return {};
53175323
}
@@ -5421,7 +5427,7 @@ void SwiftASTContext::PrintDiagnostics(DiagnosticManager &diagnostic_manager,
54215427
uint32_t last_line) const {
54225428
// VALID_OR_RETURN cannot be used here here since would exit on error.
54235429
LLDB_SCOPED_TIMER();
5424-
if (!m_ast_context_ap.get()) {
5430+
if (!GetASTContext()) {
54255431
RaiseFatalError("Swift compiler could not be initialized");
54265432
return;
54275433
}
@@ -5431,7 +5437,7 @@ void SwiftASTContext::PrintDiagnostics(DiagnosticManager &diagnostic_manager,
54315437
assert(m_diagnostic_consumer_ap);
54325438
auto &diags =
54335439
*static_cast<StoringDiagnosticConsumer *>(m_diagnostic_consumer_ap.get());
5434-
if (m_ast_context_ap->Diags.hasFatalErrorOccurred() &&
5440+
if (GetASTContext()->Diags.hasFatalErrorOccurred() &&
54355441
!m_reported_fatal_error) {
54365442
DiagnosticManager fatal_diagnostics;
54375443
diags.PrintDiagnostics(fatal_diagnostics, {}, bufferID, first_line,
@@ -5518,56 +5524,57 @@ void SwiftASTContext::LogConfiguration(bool is_repl) {
55185524
// want the logs in the error case!
55195525
HEALTH_LOG_PRINTF("(SwiftASTContext*)%p:", static_cast<void *>(this));
55205526

5521-
if (!m_ast_context_ap) {
5527+
auto ast_context = GetASTContext();
5528+
if (!ast_context) {
55225529
HEALTH_LOG_PRINTF(" (no AST context)");
55235530
return;
55245531
}
55255532
if (is_repl)
55265533
HEALTH_LOG_PRINTF(" REPL : true");
55275534
HEALTH_LOG_PRINTF(" Swift/C++ interop : %s",
5528-
m_ast_context_ap->LangOpts.EnableCXXInterop ? "on" : "off");
5535+
ast_context->LangOpts.EnableCXXInterop ? "on" : "off");
55295536
HEALTH_LOG_PRINTF(" Swift/Objective-C interop : %s",
5530-
m_ast_context_ap->LangOpts.EnableObjCInterop ? "on" : "off");
5537+
ast_context->LangOpts.EnableObjCInterop ? "on" : "off");
55315538

55325539
HEALTH_LOG_PRINTF(" Architecture : %s",
5533-
m_ast_context_ap->LangOpts.Target.getTriple().c_str());
5540+
ast_context->LangOpts.Target.getTriple().c_str());
55345541
HEALTH_LOG_PRINTF(
55355542
" SDK path : %s",
5536-
m_ast_context_ap->SearchPathOpts.getSDKPath().str().c_str());
5543+
ast_context->SearchPathOpts.getSDKPath().str().c_str());
55375544
HEALTH_LOG_PRINTF(
55385545
" Runtime resource path : %s",
5539-
m_ast_context_ap->SearchPathOpts.RuntimeResourcePath.c_str());
5546+
ast_context->SearchPathOpts.RuntimeResourcePath.c_str());
55405547
HEALTH_LOG_PRINTF(" Runtime library paths : (%llu items)",
5541-
(unsigned long long)m_ast_context_ap->SearchPathOpts
5548+
(unsigned long long)ast_context->SearchPathOpts
55425549
.RuntimeLibraryPaths.size());
55435550

55445551
for (const auto &runtime_library_path :
5545-
m_ast_context_ap->SearchPathOpts.RuntimeLibraryPaths)
5552+
ast_context->SearchPathOpts.RuntimeLibraryPaths)
55465553
HEALTH_LOG_PRINTF(" %s", runtime_library_path.c_str());
55475554

55485555
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
55505557
.getRuntimeLibraryImportPaths()
55515558
.size());
55525559

55535560
for (const auto &runtime_import_path :
5554-
m_ast_context_ap->SearchPathOpts.getRuntimeLibraryImportPaths())
5561+
ast_context->SearchPathOpts.getRuntimeLibraryImportPaths())
55555562
HEALTH_LOG_PRINTF(" %s", runtime_import_path.c_str());
55565563

55575564
HEALTH_LOG_PRINTF(" Framework search paths : (%llu items)",
5558-
(unsigned long long)m_ast_context_ap->SearchPathOpts
5565+
(unsigned long long)ast_context->SearchPathOpts
55595566
.getFrameworkSearchPaths()
55605567
.size());
55615568
for (const auto &framework_search_path :
5562-
m_ast_context_ap->SearchPathOpts.getFrameworkSearchPaths())
5569+
ast_context->SearchPathOpts.getFrameworkSearchPaths())
55635570
HEALTH_LOG_PRINTF(" %s", framework_search_path.Path.c_str());
55645571

55655572
HEALTH_LOG_PRINTF(" Import search paths : (%llu items)",
5566-
(unsigned long long)m_ast_context_ap->SearchPathOpts
5573+
(unsigned long long)ast_context->SearchPathOpts
55675574
.getImportSearchPaths()
55685575
.size());
55695576
for (const auto &import_search_path :
5570-
m_ast_context_ap->SearchPathOpts.getImportSearchPaths())
5577+
ast_context->SearchPathOpts.getImportSearchPaths())
55715578
HEALTH_LOG_PRINTF(" %s", import_search_path.Path.c_str());
55725579

55735580
swift::ClangImporterOptions &clang_importer_options =
@@ -5587,9 +5594,9 @@ void SwiftASTContext::LogConfiguration(bool is_repl) {
55875594
HEALTH_LOG_PRINTF(" %s", extra_arg.c_str());
55885595

55895596
HEALTH_LOG_PRINTF(" Plugin search options : (%llu items)",
5590-
(unsigned long long)m_ast_context_ap->SearchPathOpts
5597+
(unsigned long long)ast_context->SearchPathOpts
55915598
.PluginSearchOpts.size());
5592-
for (auto &elem : m_ast_context_ap->SearchPathOpts.PluginSearchOpts) {
5599+
for (auto &elem : ast_context->SearchPathOpts.PluginSearchOpts) {
55935600
if (auto *opt =
55945601
elem.dyn_cast<swift::PluginSearchOption::LoadPluginLibrary>()) {
55955602
HEALTH_LOG_PRINTF(" -load-plugin-library %s",
@@ -8938,21 +8945,21 @@ SwiftASTContextForExpressions::SwiftASTContextForExpressions(
89388945

89398946
SwiftASTContextForExpressions::~SwiftASTContextForExpressions() {
89408947
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)
89438950
return;
89448951
// A RemoteASTContext associated with this swift::ASTContext has
89458952
// to be destroyed before the swift::ASTContext is destroyed.
89468953
if (TargetSP target_sp = GetTargetWP().lock()) {
89478954
if (ProcessSP process_sp = target_sp->GetProcessSP())
89488955
if (auto *runtime = SwiftLanguageRuntime::Get(process_sp))
8949-
runtime->ReleaseAssociatedRemoteASTContext(ctx);
8956+
runtime->ReleaseAssociatedRemoteASTContext(*swift_context);
89508957
} else {
89518958
LOG_PRINTF(GetLog(LLDBLog::Types | LLDBLog::Expressions),
89528959
"Failed to lock target in ~SwiftASTContextForExpressions().");
89538960
}
89548961

8955-
GetASTMap().Erase(ctx);
8962+
GetASTMap().Erase(*swift_context);
89568963
}
89578964

89588965
PersistentExpressionState *

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

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

272272
ThreadSafeASTContext GetASTContext();
273273

274+
ThreadSafeASTContext GetASTContext() const;
275+
274276
swift::IRGenDebugInfoLevel GetGenerateDebugInfo();
275277

276278
static swift::PrintOptions
@@ -313,8 +315,6 @@ class SwiftASTContext : public TypeSystemSwift {
313315
m_platform_sdk_path = path.str();
314316
}
315317

316-
const swift::SearchPathOptions *GetSearchPathOptions() const;
317-
318318
/// \return the ExtraArgs of the ClangImporterOptions.
319319
const std::vector<std::string> &GetClangArguments();
320320

0 commit comments

Comments
 (0)