@@ -656,64 +656,61 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
656
656
657
657
Status err;
658
658
659
+ auto error = [&](const char *error_msg, const char *detail = nullptr ) {
660
+ if (detail)
661
+ LLDB_LOG (log, " %s: %s" , error_msg, detail);
662
+ else
663
+ LLDB_LOG (log, error_msg);
664
+
665
+ diagnostic_manager.PutString (eDiagnosticSeverityError, error_msg);
666
+ if (detail)
667
+ diagnostic_manager.AppendMessageToDiagnostic (detail);
668
+ return false ;
669
+ };
670
+
659
671
InstallContext (exe_ctx);
660
672
Target *target = exe_ctx.GetTargetPtr ();
661
- if (!target) {
662
- diagnostic_manager.PutString (eDiagnosticSeverityError,
663
- " couldn't start parsing (no target)" );
664
- return false ;
665
- }
673
+ if (!target)
674
+ return error (" couldn't start parsing (no target)" );
666
675
667
676
StackFrame *frame = exe_ctx.GetFramePtr ();
668
- if (!frame) {
669
- diagnostic_manager.PutString (eDiagnosticSeverityError,
670
- " couldn't start parsing - no stack frame" );
671
- LLDB_LOG (log, " no stack frame" );
672
- return false ;
673
- }
677
+ if (!frame)
678
+ return error (" couldn't start parsing - no stack frame" );
674
679
675
680
auto *exe_scope = exe_ctx.GetBestExecutionContextScope ();
676
- if (!exe_scope) {
677
- LLDB_LOG (log, " no execution context scope" );
678
- return false ;
679
- }
681
+ if (!exe_scope)
682
+ return error ( " no execution context scope" );
680
683
681
684
m_swift_scratch_ctx = target->GetSwiftScratchContext (m_err, *exe_scope);
682
- if (!m_swift_scratch_ctx) {
683
- LLDB_LOG (log, " no scratch context" , m_err. AsCString ());
684
- return false ;
685
- }
685
+ if (!m_swift_scratch_ctx)
686
+ return error ( " could not create a Swift scratch context: " ,
687
+ m_err. AsCString ()) ;
688
+
686
689
m_swift_ast_ctx = llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(
687
690
m_swift_scratch_ctx->get ()->GetSwiftASTContext ());
688
691
689
- if (!m_swift_ast_ctx) {
690
- LLDB_LOG (log, " no Swift AST context" );
691
- return false ;
692
- }
692
+ if (!m_swift_ast_ctx)
693
+ return error (" could not create a Swift AST context" );
693
694
694
695
if (m_swift_ast_ctx->HasFatalErrors ()) {
696
+ m_swift_ast_ctx->PrintDiagnostics (diagnostic_manager);
695
697
LLDB_LOG (log, " Swift AST context is in a fatal error state" );
696
698
return false ;
697
699
}
698
700
699
701
// This may destroy the scratch context.
700
702
auto *persistent_state = GetPersistentState (target, exe_ctx);
701
- if (!persistent_state) {
702
- diagnostic_manager.PutString (eDiagnosticSeverityError,
703
- " couldn't start parsing (no persistent data)" );
704
- return false ;
705
- }
703
+ if (!persistent_state)
704
+ return error (" could not start parsing (no persistent data)" );
706
705
707
- Status error ;
706
+ Status status ;
708
707
SourceModule module_info;
709
708
module_info.path .emplace_back (" Swift" );
710
709
swift::ModuleDecl *module_decl =
711
- m_swift_ast_ctx->GetModule (module_info, error );
710
+ m_swift_ast_ctx->GetModule (module_info, status );
712
711
713
- if (error.Fail () || !module_decl) {
714
- LLDB_LOG (log, " couldn't load Swift Standard Library\n " );
715
- return false ;
716
- }
712
+ if (status.Fail () || !module_decl)
713
+ return error (" could not load Swift Standard Library" , status.AsCString ());
717
714
718
715
persistent_state->AddHandLoadedModule (ConstString (" Swift" ),
719
716
swift::ImportedModule (module_decl));
@@ -722,10 +719,9 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
722
719
723
720
ScanContext (exe_ctx, err);
724
721
725
- if (!err.Success ()) {
726
- diagnostic_manager.Printf (eDiagnosticSeverityError , " warning: %s\n " ,
722
+ if (!err.Success ())
723
+ diagnostic_manager.Printf (eDiagnosticSeverityWarning , " warning: %s\n " ,
727
724
err.AsCString ());
728
- }
729
725
730
726
StreamString m_transformed_stream;
731
727
@@ -748,7 +744,8 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
748
744
749
745
while (true ) {
750
746
SwiftExpressionParser::ParseResult parse_result =
751
- GetTextAndSetExpressionParser (diagnostic_manager, source_code, exe_ctx, exe_scope);
747
+ GetTextAndSetExpressionParser (diagnostic_manager, source_code, exe_ctx,
748
+ exe_scope);
752
749
753
750
if (parse_result == ParseResult::success)
754
751
break ;
@@ -844,15 +841,12 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
844
841
if (process && m_jit_start_addr != LLDB_INVALID_ADDRESS)
845
842
m_jit_process_wp = lldb::ProcessWP (process->shared_from_this ());
846
843
return true ;
847
- } else {
848
- const char *error_cstr = jit_error.AsCString ();
849
- if (error_cstr && error_cstr[0 ])
850
- diagnostic_manager.PutString (eDiagnosticSeverityError, error_cstr);
851
- else
852
- diagnostic_manager.PutString (eDiagnosticSeverityError,
853
- " expression can't be interpreted or run\n " );
854
- return false ;
855
844
}
845
+
846
+ const char *error_cstr = jit_error.AsCString ();
847
+ if (!error_cstr || !error_cstr[0 ])
848
+ error_cstr = " expression can't be interpreted or run" ;
849
+ return error (error_cstr);
856
850
}
857
851
858
852
bool SwiftUserExpression::AddArguments (ExecutionContext &exe_ctx,
0 commit comments