@@ -417,7 +417,6 @@ void ClangUserExpression::CreateSourceCode(
417
417
DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
418
418
std::vector<std::string> modules_to_import, bool for_completion) {
419
419
420
- m_filename = m_clang_state->GetNextExprFileName ();
421
420
std::string prefix = m_expr_prefix;
422
421
423
422
if (m_options.GetExecutionPolicy () == eExecutionPolicyTopLevel) {
@@ -477,9 +476,6 @@ CppModuleConfiguration GetModuleConfig(lldb::LanguageType language,
477
476
if (!target)
478
477
return LogConfigError (" No target" );
479
478
480
- if (!target->GetEnableImportStdModule ())
481
- return LogConfigError (" Importing std module not enabled in settings" );
482
-
483
479
StackFrame *frame = exe_ctx.GetFramePtr ();
484
480
if (!frame)
485
481
return LogConfigError (" No frame" );
@@ -529,8 +525,6 @@ CppModuleConfiguration GetModuleConfig(lldb::LanguageType language,
529
525
bool ClangUserExpression::PrepareForParsing (
530
526
DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
531
527
bool for_completion) {
532
- Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
533
-
534
528
InstallContext (exe_ctx);
535
529
536
530
if (!SetupPersistentState (diagnostic_manager, exe_ctx))
@@ -551,50 +545,20 @@ bool ClangUserExpression::PrepareForParsing(
551
545
552
546
SetupDeclVendor (exe_ctx, m_target, diagnostic_manager);
553
547
554
- CppModuleConfiguration module_config = GetModuleConfig (m_language, exe_ctx);
555
- llvm::ArrayRef<std::string> imported_modules =
556
- module_config.GetImportedModules ();
557
- m_imported_cpp_modules = !imported_modules.empty ();
558
- m_include_directories = module_config.GetIncludeDirs ();
548
+ m_filename = m_clang_state->GetNextExprFileName ();
559
549
560
- LLDB_LOG (log, " List of imported modules in expression: {0}" ,
561
- llvm::make_range (imported_modules.begin (), imported_modules.end ()));
562
- LLDB_LOG (log, " List of include directories gathered for modules: {0}" ,
563
- llvm::make_range (m_include_directories.begin (),
564
- m_include_directories.end ()));
550
+ if (m_target->GetImportStdModule () == eImportStdModuleTrue)
551
+ SetupCppModuleImports (exe_ctx);
565
552
566
- CreateSourceCode (diagnostic_manager, exe_ctx, imported_modules ,
553
+ CreateSourceCode (diagnostic_manager, exe_ctx, m_imported_cpp_modules ,
567
554
for_completion);
568
555
return true ;
569
556
}
570
557
571
- bool ClangUserExpression::Parse (DiagnosticManager &diagnostic_manager,
572
- ExecutionContext &exe_ctx,
573
- lldb_private::ExecutionPolicy execution_policy,
574
- bool keep_result_in_memory,
575
- bool generate_debug_info) {
576
- Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
577
-
578
- if (!PrepareForParsing (diagnostic_manager, exe_ctx, /* for_completion*/ false ))
579
- return false ;
580
-
581
- LLDB_LOGF (log, " Parsing the following code:\n %s" , m_transformed_text.c_str ());
582
-
583
- // //////////////////////////////////
584
- // Set up the target and compiler
585
- //
586
-
587
- Target *target = exe_ctx.GetTargetPtr ();
588
-
589
- if (!target) {
590
- diagnostic_manager.PutString (eDiagnosticSeverityError, " invalid target" );
591
- return false ;
592
- }
593
-
594
- // ////////////////////////
595
- // Parse the expression
596
- //
597
-
558
+ bool ClangUserExpression::TryParse (
559
+ DiagnosticManager &diagnostic_manager, ExecutionContextScope *exe_scope,
560
+ ExecutionContext &exe_ctx, lldb_private::ExecutionPolicy execution_policy,
561
+ bool keep_result_in_memory, bool generate_debug_info) {
598
562
m_materializer_up = std::make_unique<Materializer>();
599
563
600
564
ResetDeclMap (exe_ctx, m_result_delegate, keep_result_in_memory);
@@ -612,26 +576,16 @@ bool ClangUserExpression::Parse(DiagnosticManager &diagnostic_manager,
612
576
DeclMap ()->SetLookupsEnabled (true );
613
577
}
614
578
615
- Process *process = exe_ctx.GetProcessPtr ();
616
- ExecutionContextScope *exe_scope = process;
617
-
618
- if (!exe_scope)
619
- exe_scope = exe_ctx.GetTargetPtr ();
620
-
621
- // We use a shared pointer here so we can use the original parser - if it
622
- // succeeds or the rewrite parser we might make if it fails. But the
623
- // parser_sp will never be empty.
624
-
625
- ClangExpressionParser parser (exe_scope, *this , generate_debug_info,
626
- m_include_directories, m_filename);
579
+ m_parser = std::make_unique<ClangExpressionParser>(
580
+ exe_scope, *this , generate_debug_info, m_include_directories, m_filename);
627
581
628
- unsigned num_errors = parser. Parse (diagnostic_manager);
582
+ unsigned num_errors = m_parser-> Parse (diagnostic_manager);
629
583
630
584
// Check here for FixItHints. If there are any try to apply the fixits and
631
585
// set the fixed text in m_fixed_text before returning an error.
632
586
if (num_errors) {
633
587
if (diagnostic_manager.HasFixIts ()) {
634
- if (parser. RewriteExpression (diagnostic_manager)) {
588
+ if (m_parser-> RewriteExpression (diagnostic_manager)) {
635
589
size_t fixed_start;
636
590
size_t fixed_end;
637
591
m_fixed_text = diagnostic_manager.GetFixedExpression ();
@@ -652,7 +606,7 @@ bool ClangUserExpression::Parse(DiagnosticManager &diagnostic_manager,
652
606
//
653
607
654
608
{
655
- Status jit_error = parser. PrepareForExecution (
609
+ Status jit_error = m_parser-> PrepareForExecution (
656
610
m_jit_start_addr, m_jit_end_addr, m_execution_unit_sp, exe_ctx,
657
611
m_can_interpret, execution_policy);
658
612
@@ -666,10 +620,91 @@ bool ClangUserExpression::Parse(DiagnosticManager &diagnostic_manager,
666
620
return false ;
667
621
}
668
622
}
623
+ return true ;
624
+ }
625
+
626
+ void ClangUserExpression::SetupCppModuleImports (ExecutionContext &exe_ctx) {
627
+ Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
628
+
629
+ CppModuleConfiguration module_config = GetModuleConfig (m_language, exe_ctx);
630
+ m_imported_cpp_modules = module_config.GetImportedModules ();
631
+ m_include_directories = module_config.GetIncludeDirs ();
632
+
633
+ LLDB_LOG (log, " List of imported modules in expression: {0}" ,
634
+ llvm::make_range (m_imported_cpp_modules.begin (),
635
+ m_imported_cpp_modules.end ()));
636
+ LLDB_LOG (log, " List of include directories gathered for modules: {0}" ,
637
+ llvm::make_range (m_include_directories.begin (),
638
+ m_include_directories.end ()));
639
+ }
640
+
641
+ static bool shouldRetryWithCppModule (Target &target, ExecutionPolicy exe_policy) {
642
+ // Top-level expression don't yet support importing C++ modules.
643
+ if (exe_policy == ExecutionPolicy::eExecutionPolicyTopLevel)
644
+ return false ;
645
+ return target.GetImportStdModule () == eImportStdModuleFallback;
646
+ }
647
+
648
+ bool ClangUserExpression::Parse (DiagnosticManager &diagnostic_manager,
649
+ ExecutionContext &exe_ctx,
650
+ lldb_private::ExecutionPolicy execution_policy,
651
+ bool keep_result_in_memory,
652
+ bool generate_debug_info) {
653
+ Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
654
+
655
+ if (!PrepareForParsing (diagnostic_manager, exe_ctx, /* for_completion*/ false ))
656
+ return false ;
657
+
658
+ LLDB_LOGF (log, " Parsing the following code:\n %s" , m_transformed_text.c_str ());
659
+
660
+ // //////////////////////////////////
661
+ // Set up the target and compiler
662
+ //
663
+
664
+ Target *target = exe_ctx.GetTargetPtr ();
665
+
666
+ if (!target) {
667
+ diagnostic_manager.PutString (eDiagnosticSeverityError, " invalid target" );
668
+ return false ;
669
+ }
670
+
671
+ // ////////////////////////
672
+ // Parse the expression
673
+ //
674
+
675
+ Process *process = exe_ctx.GetProcessPtr ();
676
+ ExecutionContextScope *exe_scope = process;
677
+
678
+ if (!exe_scope)
679
+ exe_scope = exe_ctx.GetTargetPtr ();
680
+
681
+ bool parse_success = TryParse (diagnostic_manager, exe_scope, exe_ctx,
682
+ execution_policy, keep_result_in_memory,
683
+ generate_debug_info);
684
+ // If the expression failed to parse, check if retrying parsing with a loaded
685
+ // C++ module is possible.
686
+ if (!parse_success && shouldRetryWithCppModule (*target, execution_policy)) {
687
+ // Load the loaded C++ modules.
688
+ SetupCppModuleImports (exe_ctx);
689
+ // If we did load any modules, then retry parsing.
690
+ if (!m_imported_cpp_modules.empty ()) {
691
+ // The module imports are injected into the source code wrapper,
692
+ // so recreate those.
693
+ CreateSourceCode (diagnostic_manager, exe_ctx, m_imported_cpp_modules,
694
+ /* for_completion*/ false );
695
+ // Clear the error diagnostics from the previous parse attempt.
696
+ diagnostic_manager.Clear ();
697
+ parse_success = TryParse (diagnostic_manager, exe_scope, exe_ctx,
698
+ execution_policy, keep_result_in_memory,
699
+ generate_debug_info);
700
+ }
701
+ }
702
+ if (!parse_success)
703
+ return false ;
669
704
670
705
if (exe_ctx.GetProcessPtr () && execution_policy == eExecutionPolicyTopLevel) {
671
706
Status static_init_error =
672
- parser. RunStaticInitializers (m_execution_unit_sp, exe_ctx);
707
+ m_parser-> RunStaticInitializers (m_execution_unit_sp, exe_ctx);
673
708
674
709
if (!static_init_error.Success ()) {
675
710
const char *error_cstr = static_init_error.AsCString ();
0 commit comments