Skip to content

Commit e78c5f2

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 94a95ed commit e78c5f2

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
@@ -1079,16 +1079,16 @@ void SwiftASTContext::SetCompilerInvocationLLDBOverrides() {
10791079
}
10801080

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

10881088

10891089
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);
10921092
}
10931093

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

25822582
// Report progress on module importing by using a callback function in
25832583
// swift::ASTContext
2584+
auto ast_context = swift_ast_sp->GetASTContext();
25842585
Progress progress("Importing Swift standard library");
2585-
swift_ast_sp->m_ast_context_ap->SetPreModuleImportCallback(
2586+
ast_context->SetPreModuleImportCallback(
25862587
[&progress](llvm::StringRef module_name,
25872588
swift::ASTContext::ModuleImportKind kind) {
25882589
progress.Increment(1, module_name.str());
@@ -2591,13 +2592,13 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
25912592
// Clear the callback function on scope exit to prevent an out-of-scope
25922593
// access of the progress local variable
25932594
auto on_exit = llvm::make_scope_exit([&]() {
2594-
swift_ast_sp->m_ast_context_ap->SetPreModuleImportCallback(
2595+
ast_context->SetPreModuleImportCallback(
25952596
[](llvm::StringRef module_name,
25962597
swift::ASTContext::ModuleImportKind kind) {});
25972598
});
25982599

25992600
swift::ModuleDecl *stdlib =
2600-
swift_ast_sp->m_ast_context_ap->getStdlibModule(can_create);
2601+
ast_context->getStdlibModule(can_create);
26012602
if (!stdlib || IsDWARFImported(*stdlib)) {
26022603
logError("couldn't load the Swift stdlib");
26032604
return {};
@@ -3149,10 +3150,11 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
31493150
}
31503151

31513152
{
3153+
auto ast_context = swift_ast_sp->GetASTContext();
31523154
// Report progress on module importing by using a callback function in
31533155
// swift::ASTContext
31543156
Progress progress("Importing Swift standard library");
3155-
swift_ast_sp->m_ast_context_ap->SetPreModuleImportCallback(
3157+
ast_context->SetPreModuleImportCallback(
31563158
[&progress](llvm::StringRef module_name,
31573159
swift::ASTContext::ModuleImportKind kind) {
31583160
progress.Increment(1, module_name.str());
@@ -3161,14 +3163,14 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
31613163
// Clear the callback function on scope exit to prevent an out-of-scope
31623164
// access of the progress local variable
31633165
auto on_exit = llvm::make_scope_exit([&]() {
3164-
swift_ast_sp->m_ast_context_ap->SetPreModuleImportCallback(
3166+
ast_context->SetPreModuleImportCallback(
31653167
[](llvm::StringRef module_name,
31663168
swift::ASTContext::ModuleImportKind kind) {});
31673169
});
31683170

31693171
const bool can_create = true;
31703172
swift::ModuleDecl *stdlib =
3171-
swift_ast_sp->m_ast_context_ap->getStdlibModule(can_create);
3173+
ast_context->getStdlibModule(can_create);
31723174
if (!stdlib || IsDWARFImported(*stdlib)) {
31733175
logError("couldn't load the Swift stdlib");
31743176
return {};
@@ -3360,10 +3362,17 @@ bool SwiftASTContext::SetTriple(const llvm::Triple triple, Module *module) {
33603362

33613363
m_compiler_invocation_ap->setTargetTriple(adjusted_triple);
33623364

3365+
#ifndef NDEBUG
33633366
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();
33643371
assert(!m_ast_context_ap ||
33653372
(llvm::Triple(m_ast_context_ap->LangOpts.Target.getTriple()) ==
33663373
adjusted_triple));
3374+
m_ast_context_mutex.unlock();
3375+
#endif
33673376

33683377
// Every time the triple is changed the LangOpts must be updated
33693378
// too, because Swift default-initializes the EnableObjCInterop
@@ -3777,6 +3786,10 @@ ThreadSafeASTContext SwiftASTContext::GetASTContext() {
37773786
return {m_ast_context_ap.get(), m_ast_context_mutex};
37783787
}
37793788

3789+
ThreadSafeASTContext SwiftASTContext::GetASTContext() const {
3790+
return const_cast<SwiftASTContext *>(this)->GetASTContext();
3791+
}
3792+
37803793
swift::MemoryBufferSerializedModuleLoader *
37813794
SwiftASTContext::GetMemoryBufferModuleLoader() {
37823795
VALID_OR_RETURN(nullptr);
@@ -3792,14 +3805,6 @@ swift::ClangImporter *SwiftASTContext::GetClangImporter() {
37923805
return m_clangimporter;
37933806
}
37943807

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-
38033808
const std::vector<std::string> &SwiftASTContext::GetClangArguments() {
38043809
return GetClangImporterOptions().ExtraArgs;
38053810
}
@@ -4782,8 +4787,7 @@ SwiftASTContext::ReconstructType(ConstString mangled_typename) {
47824787

47834788
CompilerType SwiftASTContext::GetAnyObjectType() {
47844789
VALID_OR_RETURN(CompilerType());
4785-
ThreadSafeASTContext ast = GetASTContext();
4786-
return ToCompilerType({ast->getAnyObjectType()});
4790+
return ToCompilerType({GetASTContext()->getAnyObjectType()});
47874791
}
47884792

47894793
static CompilerType ValueDeclToType(swift::ValueDecl *decl) {
@@ -4915,7 +4919,7 @@ SwiftASTContext::FindContainedTypeOrDecl(llvm::StringRef name,
49154919
return 0;
49164920
swift::NominalTypeDecl *nominal_decl = nominal_type->getDecl();
49174921
llvm::ArrayRef<swift::ValueDecl *> decls = nominal_decl->lookupDirect(
4918-
swift::DeclName(m_ast_context_ap->getIdentifier(name)));
4922+
swift::DeclName(GetASTContext()->getIdentifier(name)));
49194923
for (auto *decl : decls)
49204924
results.emplace(DeclToTypeOrDecl(decl));
49214925
}
@@ -5053,7 +5057,8 @@ size_t SwiftASTContext::FindType(const char *name,
50535057
CompilerType SwiftASTContext::ImportType(CompilerType &type, Status &error) {
50545058
VALID_OR_RETURN(CompilerType());
50555059

5056-
if (m_ast_context_ap.get() == NULL)
5060+
auto ast_context = GetASTContext();
5061+
if (!ast_context)
50575062
return CompilerType();
50585063

50595064
auto ts = type.GetTypeSystem();
@@ -5126,7 +5131,7 @@ swift::ModuleDecl *SwiftASTContext::GetScratchModule() {
51265131
if (m_scratch_module == nullptr) {
51275132
ThreadSafeASTContext ast_ctx = GetASTContext();
51285133
m_scratch_module = swift::ModuleDecl::createEmpty(
5129-
GetASTContext()->getIdentifier("__lldb_scratch_module"), **ast_ctx);
5134+
ast_ctx->getIdentifier("__lldb_scratch_module"), **ast_ctx);
51305135
}
51315136
return m_scratch_module;
51325137
}
@@ -5223,12 +5228,13 @@ SwiftASTContext::CreateTupleType(const std::vector<TupleElement> &elements) {
52235228
std::vector<swift::TupleTypeElt> tuple_elems;
52245229
for (const TupleElement &element : elements) {
52255230
if (auto swift_type = GetSwiftTypeIgnoringErrors(element.element_type)) {
5226-
if (element.element_name.IsEmpty())
5231+
if (element.element_name.IsEmpty()) {
52275232
tuple_elems.push_back(swift::TupleTypeElt(swift_type));
5228-
else
5233+
} else {
52295234
tuple_elems.push_back(swift::TupleTypeElt(
5230-
swift_type, m_ast_context_ap->getIdentifier(
5235+
swift_type, GetASTContext()->getIdentifier(
52315236
element.element_name.GetCString())));
5237+
}
52325238
} else
52335239
return {};
52345240
}
@@ -5351,7 +5357,7 @@ void SwiftASTContext::PrintDiagnostics(DiagnosticManager &diagnostic_manager,
53515357
uint32_t bufferID, uint32_t first_line,
53525358
uint32_t last_line) const {
53535359
// VALID_OR_RETURN cannot be used here here since would exit on error.
5354-
if (!m_ast_context_ap.get()) {
5360+
if (!GetASTContext()) {
53555361
RaiseFatalError("Swift compiler could not be initialized");
53565362
return;
53575363
}
@@ -5361,7 +5367,7 @@ void SwiftASTContext::PrintDiagnostics(DiagnosticManager &diagnostic_manager,
53615367
assert(m_diagnostic_consumer_ap);
53625368
auto &diags =
53635369
*static_cast<StoringDiagnosticConsumer *>(m_diagnostic_consumer_ap.get());
5364-
if (m_ast_context_ap->Diags.hasFatalErrorOccurred() &&
5370+
if (GetASTContext()->Diags.hasFatalErrorOccurred() &&
53655371
!m_reported_fatal_error) {
53665372
DiagnosticManager fatal_diagnostics;
53675373
diags.PrintDiagnostics(fatal_diagnostics, {}, bufferID, first_line,
@@ -5448,56 +5454,57 @@ void SwiftASTContext::LogConfiguration(bool is_repl) {
54485454
// want the logs in the error case!
54495455
HEALTH_LOG_PRINTF("(SwiftASTContext*)%p:", static_cast<void *>(this));
54505456

5451-
if (!m_ast_context_ap) {
5457+
auto ast_context = GetASTContext();
5458+
if (!ast_context) {
54525459
HEALTH_LOG_PRINTF(" (no AST context)");
54535460
return;
54545461
}
54555462
if (is_repl)
54565463
HEALTH_LOG_PRINTF(" REPL : true");
54575464
HEALTH_LOG_PRINTF(" Swift/C++ interop : %s",
5458-
m_ast_context_ap->LangOpts.EnableCXXInterop ? "on" : "off");
5465+
ast_context->LangOpts.EnableCXXInterop ? "on" : "off");
54595466
HEALTH_LOG_PRINTF(" Swift/Objective-C interop : %s",
5460-
m_ast_context_ap->LangOpts.EnableObjCInterop ? "on" : "off");
5467+
ast_context->LangOpts.EnableObjCInterop ? "on" : "off");
54615468

54625469
HEALTH_LOG_PRINTF(" Architecture : %s",
5463-
m_ast_context_ap->LangOpts.Target.getTriple().c_str());
5470+
ast_context->LangOpts.Target.getTriple().c_str());
54645471
HEALTH_LOG_PRINTF(
54655472
" SDK path : %s",
5466-
m_ast_context_ap->SearchPathOpts.getSDKPath().str().c_str());
5473+
ast_context->SearchPathOpts.getSDKPath().str().c_str());
54675474
HEALTH_LOG_PRINTF(
54685475
" Runtime resource path : %s",
5469-
m_ast_context_ap->SearchPathOpts.RuntimeResourcePath.c_str());
5476+
ast_context->SearchPathOpts.RuntimeResourcePath.c_str());
54705477
HEALTH_LOG_PRINTF(" Runtime library paths : (%llu items)",
5471-
(unsigned long long)m_ast_context_ap->SearchPathOpts
5478+
(unsigned long long)ast_context->SearchPathOpts
54725479
.RuntimeLibraryPaths.size());
54735480

54745481
for (const auto &runtime_library_path :
5475-
m_ast_context_ap->SearchPathOpts.RuntimeLibraryPaths)
5482+
ast_context->SearchPathOpts.RuntimeLibraryPaths)
54765483
HEALTH_LOG_PRINTF(" %s", runtime_library_path.c_str());
54775484

54785485
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
54805487
.getRuntimeLibraryImportPaths()
54815488
.size());
54825489

54835490
for (const auto &runtime_import_path :
5484-
m_ast_context_ap->SearchPathOpts.getRuntimeLibraryImportPaths())
5491+
ast_context->SearchPathOpts.getRuntimeLibraryImportPaths())
54855492
HEALTH_LOG_PRINTF(" %s", runtime_import_path.c_str());
54865493

54875494
HEALTH_LOG_PRINTF(" Framework search paths : (%llu items)",
5488-
(unsigned long long)m_ast_context_ap->SearchPathOpts
5495+
(unsigned long long)ast_context->SearchPathOpts
54895496
.getFrameworkSearchPaths()
54905497
.size());
54915498
for (const auto &framework_search_path :
5492-
m_ast_context_ap->SearchPathOpts.getFrameworkSearchPaths())
5499+
ast_context->SearchPathOpts.getFrameworkSearchPaths())
54935500
HEALTH_LOG_PRINTF(" %s", framework_search_path.Path.c_str());
54945501

54955502
HEALTH_LOG_PRINTF(" Import search paths : (%llu items)",
5496-
(unsigned long long)m_ast_context_ap->SearchPathOpts
5503+
(unsigned long long)ast_context->SearchPathOpts
54975504
.getImportSearchPaths()
54985505
.size());
54995506
for (const auto &import_search_path :
5500-
m_ast_context_ap->SearchPathOpts.getImportSearchPaths())
5507+
ast_context->SearchPathOpts.getImportSearchPaths())
55015508
HEALTH_LOG_PRINTF(" %s", import_search_path.Path.c_str());
55025509

55035510
swift::ClangImporterOptions &clang_importer_options =
@@ -5517,9 +5524,9 @@ void SwiftASTContext::LogConfiguration(bool is_repl) {
55175524
HEALTH_LOG_PRINTF(" %s", extra_arg.c_str());
55185525

55195526
HEALTH_LOG_PRINTF(" Plugin search options : (%llu items)",
5520-
(unsigned long long)m_ast_context_ap->SearchPathOpts
5527+
(unsigned long long)ast_context->SearchPathOpts
55215528
.PluginSearchOpts.size());
5522-
for (auto &elem : m_ast_context_ap->SearchPathOpts.PluginSearchOpts) {
5529+
for (auto &elem : ast_context->SearchPathOpts.PluginSearchOpts) {
55235530
if (auto *opt =
55245531
elem.dyn_cast<swift::PluginSearchOption::LoadPluginLibrary>()) {
55255532
HEALTH_LOG_PRINTF(" -load-plugin-library %s",
@@ -8850,21 +8857,21 @@ SwiftASTContextForExpressions::SwiftASTContextForExpressions(
88508857

88518858
SwiftASTContextForExpressions::~SwiftASTContextForExpressions() {
88528859
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)
88558862
return;
88568863
// A RemoteASTContext associated with this swift::ASTContext has
88578864
// to be destroyed before the swift::ASTContext is destroyed.
88588865
if (TargetSP target_sp = GetTargetWP().lock()) {
88598866
if (ProcessSP process_sp = target_sp->GetProcessSP())
88608867
if (auto *runtime = SwiftLanguageRuntime::Get(process_sp))
8861-
runtime->ReleaseAssociatedRemoteASTContext(ctx);
8868+
runtime->ReleaseAssociatedRemoteASTContext(*swift_context);
88628869
} else {
88638870
LOG_PRINTF(GetLog(LLDBLog::Types | LLDBLog::Expressions),
88648871
"Failed to lock target in ~SwiftASTContextForExpressions().");
88658872
}
88668873

8867-
GetASTMap().Erase(ctx);
8874+
GetASTMap().Erase(*swift_context);
88688875
}
88698876

88708877
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)