@@ -774,76 +774,79 @@ static void ResolveSpecialNames(
774
774
}
775
775
}
776
776
777
- // / Initialize the SwiftASTContext and return the wrapped
778
- // / swift::ASTContext when successful.
779
- ThreadSafeASTContext SwiftExpressionParser::SetupASTContext (
777
+ ThreadSafeASTContext SwiftExpressionParser::GetASTContext (
780
778
DiagnosticManager &diagnostic_manager,
781
779
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
+ }
817
792
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
+ }
823
798
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
+ }
826
806
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
+ }
829
812
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 ();
847
850
}
848
851
849
852
// / Returns the buffer_id for the expression's source code.
@@ -1256,7 +1259,7 @@ SwiftExpressionParser::ParseAndImport(
1256
1259
return !ObjCLanguageRuntime::Get (*process_sp);
1257
1260
};
1258
1261
1259
- ThreadSafeASTContext ast_context = SetupASTContext (
1262
+ ThreadSafeASTContext ast_context = GetASTContext (
1260
1263
diagnostic_manager, should_disable_objc_runtime, repl, playground);
1261
1264
if (!ast_context)
1262
1265
return make_error<SwiftASTContextError>();
0 commit comments