Skip to content

Commit 97da402

Browse files
committed
[NFC][lldb] Refactor SetupASTContext to GetASTContext
Refactor SetupASTContext so it can be called multiple times, and rename it to GetASTContext to indicate that change.
1 parent 75a0bac commit 97da402

File tree

2 files changed

+79
-69
lines changed

2 files changed

+79
-69
lines changed

lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp

Lines changed: 68 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -774,76 +774,79 @@ static void ResolveSpecialNames(
774774
}
775775
}
776776

777-
/// Initialize the SwiftASTContext and return the wrapped
778-
/// swift::ASTContext when successful.
779-
ThreadSafeASTContext SwiftExpressionParser::SetupASTContext(
777+
ThreadSafeASTContext SwiftExpressionParser::GetASTContext(
780778
DiagnosticManager &diagnostic_manager,
781779
std::function<bool()> disable_objc_runtime, bool repl, bool playground) {
782-
// Lazily get the clang importer if we can to make sure it exists in
783-
// case we need it.
784-
if (!m_swift_ast_ctx.GetClangImporter()) {
785-
std::string swift_error =
786-
m_swift_ast_ctx.GetFatalErrors().AsCString("error: unknown error.");
787-
diagnostic_manager.PutString(eSeverityError, swift_error);
788-
diagnostic_manager.PutString(eSeverityInfo,
789-
"Couldn't initialize Swift expression "
790-
"evaluator due to previous errors.");
791-
return ThreadSafeASTContext();
792-
}
793-
794-
if (m_swift_ast_ctx.HasFatalErrors()) {
795-
diagnostic_manager.PutString(eSeverityError,
796-
"The AST context is in a fatal error state.");
797-
return ThreadSafeASTContext();
798-
}
799-
800-
ThreadSafeASTContext ast_context = m_swift_ast_ctx.GetASTContext();
801-
if (!ast_context) {
802-
diagnostic_manager.PutString(
803-
eSeverityError,
804-
"Couldn't initialize the AST context. Please check your settings.");
805-
return ThreadSafeASTContext();
806-
}
807-
808-
if (m_swift_ast_ctx.HasFatalErrors()) {
809-
diagnostic_manager.PutString(eSeverityError,
810-
"The AST context is in a fatal error state.");
811-
return ThreadSafeASTContext();
812-
}
813-
814-
// TODO: Find a way to get contraint-solver output sent to a stream
815-
// so we can log it.
816-
// swift_ast_context.GetLanguageOptions().DebugConstraintSolver = true;
780+
llvm::call_once(m_ast_init_once_flag, [&] {
781+
// Lazily get the clang importer if we can to make sure it exists in
782+
// case we need it.
783+
if (!m_swift_ast_ctx.GetClangImporter()) {
784+
std::string swift_error =
785+
m_swift_ast_ctx.GetFatalErrors().AsCString("error: unknown error.");
786+
diagnostic_manager.PutString(eSeverityError, swift_error);
787+
diagnostic_manager.PutString(eSeverityInfo,
788+
"Couldn't initialize Swift expression "
789+
"evaluator due to previous errors.");
790+
return;
791+
}
817792

818-
// No longer part of debugger support, set it separately.
819-
m_swift_ast_ctx.GetLanguageOptions().EnableDollarIdentifiers = true;
820-
m_swift_ast_ctx.GetLanguageOptions().EnableAccessControl =
821-
(repl || playground);
822-
m_swift_ast_ctx.GetLanguageOptions().EnableTargetOSChecking = false;
793+
if (m_swift_ast_ctx.HasFatalErrors()) {
794+
diagnostic_manager.PutString(
795+
eSeverityError, "The AST context is in a fatal error state.");
796+
return;
797+
}
823798

824-
if (disable_objc_runtime())
825-
m_swift_ast_ctx.GetLanguageOptions().EnableObjCInterop = false;
799+
ThreadSafeASTContext ast_context = m_swift_ast_ctx.GetASTContext();
800+
if (!ast_context) {
801+
diagnostic_manager.PutString(
802+
eSeverityError,
803+
"Couldn't initialize the AST context. Please check your settings.");
804+
return;
805+
}
826806

827-
m_swift_ast_ctx.GetLanguageOptions().Playground = repl || playground;
828-
m_swift_ast_ctx.GetIRGenOptions().Playground = repl || playground;
807+
if (m_swift_ast_ctx.HasFatalErrors()) {
808+
diagnostic_manager.PutString(
809+
eSeverityError, "The AST context is in a fatal error state.");
810+
return;
811+
}
829812

830-
// For the expression parser and REPL we want to relax the
831-
// requirement that you put "try" in front of every expression that
832-
// might throw.
833-
if (repl || !playground)
834-
m_swift_ast_ctx.GetLanguageOptions().EnableThrowWithoutTry = true;
835-
836-
m_swift_ast_ctx.GetIRGenOptions().OutputKind =
837-
swift::IRGenOutputKind::Module;
838-
m_swift_ast_ctx.GetIRGenOptions().OptMode =
839-
swift::OptimizationMode::NoOptimization;
840-
// Normally we'd like to verify, but unfortunately the verifier's
841-
// error mode is abort().
842-
m_swift_ast_ctx.GetIRGenOptions().Verify = false;
843-
m_swift_ast_ctx.GetIRGenOptions().ForcePublicLinkage = true;
844-
845-
m_swift_ast_ctx.GetIRGenOptions().DisableRoundTripDebugTypes = true;
846-
return ast_context;
813+
// TODO: Find a way to get contraint-solver output sent to a stream
814+
// so we can log it.
815+
// swift_ast_context.GetLanguageOptions().DebugConstraintSolver = true;
816+
817+
// No longer part of debugger support, set it separately.
818+
m_swift_ast_ctx.GetLanguageOptions().EnableDollarIdentifiers = true;
819+
m_swift_ast_ctx.GetLanguageOptions().EnableAccessControl =
820+
(repl || playground);
821+
m_swift_ast_ctx.GetLanguageOptions().EnableTargetOSChecking = false;
822+
823+
if (disable_objc_runtime())
824+
m_swift_ast_ctx.GetLanguageOptions().EnableObjCInterop = false;
825+
826+
m_swift_ast_ctx.GetLanguageOptions().Playground = repl || playground;
827+
m_swift_ast_ctx.GetIRGenOptions().Playground = repl || playground;
828+
829+
// For the expression parser and REPL we want to relax the
830+
// requirement that you put "try" in front of every expression that
831+
// might throw.
832+
if (repl || !playground)
833+
m_swift_ast_ctx.GetLanguageOptions().EnableThrowWithoutTry = true;
834+
835+
m_swift_ast_ctx.GetIRGenOptions().OutputKind =
836+
swift::IRGenOutputKind::Module;
837+
m_swift_ast_ctx.GetIRGenOptions().OptMode =
838+
swift::OptimizationMode::NoOptimization;
839+
// Normally we'd like to verify, but unfortunately the verifier's
840+
// error mode is abort().
841+
m_swift_ast_ctx.GetIRGenOptions().Verify = false;
842+
m_swift_ast_ctx.GetIRGenOptions().ForcePublicLinkage = true;
843+
844+
m_swift_ast_ctx.GetIRGenOptions().DisableRoundTripDebugTypes = true;
845+
m_ast_init_successful = true;
846+
});
847+
if (m_ast_init_successful)
848+
return m_swift_ast_ctx.GetASTContext();
849+
return ThreadSafeASTContext();
847850
}
848851

849852
/// Returns the buffer_id for the expression's source code.
@@ -1256,7 +1259,7 @@ SwiftExpressionParser::ParseAndImport(
12561259
return !ObjCLanguageRuntime::Get(*process_sp);
12571260
};
12581261

1259-
ThreadSafeASTContext ast_context = SetupASTContext(
1262+
ThreadSafeASTContext ast_context = GetASTContext(
12601263
diagnostic_manager, should_disable_objc_runtime, repl, playground);
12611264
if (!ast_context)
12621265
return make_error<SwiftASTContextError>();

lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,11 @@ class SwiftExpressionParser : public ExpressionParser {
201201
unsigned &buffer_id, DiagnosticManager &diagnostic_manager,
202202
bool repl, bool playground);
203203

204-
ThreadSafeASTContext
205-
SetupASTContext(DiagnosticManager &diagnostic_manager,
206-
std::function<bool()> disable_objc_runtime, bool repl,
207-
bool playground);
204+
/// Initialize the SwiftASTContext and return the wrapped
205+
/// ThreadSafeASTContext when successful.
206+
ThreadSafeASTContext GetASTContext(DiagnosticManager &diagnostic_manager,
207+
std::function<bool()> disable_objc_runtime,
208+
bool repl, bool playground);
208209

209210
/// The expression to be parsed.
210211
Expression &m_expr;
@@ -234,6 +235,12 @@ class SwiftExpressionParser : public ExpressionParser {
234235

235236
/// Indicates whether the call to Parse of this type is cacheable.
236237
bool m_is_cacheable;
238+
239+
/// Once token to initialize the swift::ASTContext.
240+
llvm::once_flag m_ast_init_once_flag;
241+
242+
/// Indicates if the ThreadSafeASTContext was initialized correctly.
243+
bool m_ast_init_successful;
237244
};
238245

239246
class LLDBNameLookup : public swift::SILDebuggerClient {

0 commit comments

Comments
 (0)