77
77
#include " lldb/Host/HostInfo.h"
78
78
#include " lldb/Symbol/SymbolVendor.h"
79
79
#include " lldb/Target/ExecutionContext.h"
80
+ #include " lldb/Target/ExecutionContextScope.h"
80
81
#include " lldb/Target/Language.h"
81
82
#include " lldb/Target/Process.h"
82
83
#include " lldb/Target/Target.h"
@@ -425,99 +426,34 @@ static void SetupTargetOpts(CompilerInstance &compiler,
425
426
compiler.getTargetOpts ().ABI = std::move (abi);
426
427
}
427
428
428
- // ===----------------------------------------------------------------------===//
429
- // Implementation of ClangExpressionParser
430
- // ===----------------------------------------------------------------------===//
431
-
432
- ClangExpressionParser::ClangExpressionParser (
433
- ExecutionContextScope *exe_scope, Expression &expr,
434
- bool generate_debug_info, std::vector<std::string> include_directories,
435
- std::string filename)
436
- : ExpressionParser(exe_scope, expr, generate_debug_info), m_compiler(),
437
- m_pp_callbacks(nullptr ),
438
- m_include_directories(std::move(include_directories)),
439
- m_filename(std::move(filename)) {
429
+ static void SetupLangOpts (CompilerInstance &compiler,
430
+ ExecutionContextScope &exe_scope,
431
+ const Expression &expr) {
440
432
Log *log = GetLog (LLDBLog::Expressions);
441
433
442
- // We can't compile expressions without a target. So if the exe_scope is
443
- // null or doesn't have a target, then we just need to get out of here. I'll
444
- // lldbassert and not make any of the compiler objects since
445
- // I can't return errors directly from the constructor. Further calls will
446
- // check if the compiler was made and
447
- // bag out if it wasn't.
448
-
449
- if (!exe_scope) {
450
- lldbassert (exe_scope &&
451
- " Can't make an expression parser with a null scope." );
452
- return ;
453
- }
454
-
455
- lldb::TargetSP target_sp;
456
- target_sp = exe_scope->CalculateTarget ();
457
- if (!target_sp) {
458
- lldbassert (target_sp.get () &&
459
- " Can't make an expression parser with a null target." );
460
- return ;
461
- }
462
-
463
- // 1. Create a new compiler instance.
464
- m_compiler = std::make_unique<CompilerInstance>();
434
+ // If the expression is being evaluated in the context of an existing stack
435
+ // frame, we introspect to see if the language runtime is available.
465
436
466
- // Make sure clang uses the same VFS as LLDB.
467
- m_compiler-> createFileManager ( FileSystem::Instance (). GetVirtualFileSystem () );
437
+ lldb::StackFrameSP frame_sp = exe_scope. CalculateStackFrame ();
438
+ lldb::ProcessSP process_sp = exe_scope. CalculateProcess ( );
468
439
469
440
// Defaults to lldb::eLanguageTypeUnknown.
470
441
lldb::LanguageType frame_lang = expr.Language ().AsLanguageType ();
471
442
472
- // If the expression is being evaluated in the context of an existing stack
473
- // frame, we introspect to see if the language runtime is available.
474
-
475
- lldb::StackFrameSP frame_sp = exe_scope->CalculateStackFrame ();
476
- lldb::ProcessSP process_sp = exe_scope->CalculateProcess ();
477
-
478
443
// Make sure the user hasn't provided a preferred execution language with
479
444
// `expression --language X -- ...`
480
445
if (frame_sp && frame_lang == lldb::eLanguageTypeUnknown)
481
446
frame_lang = frame_sp->GetLanguage ().AsLanguageType ();
482
447
483
448
if (process_sp && frame_lang != lldb::eLanguageTypeUnknown) {
484
449
LLDB_LOGF (log, " Frame has language of type %s" ,
485
- Language::GetNameForLanguageType (frame_lang));
450
+ lldb_private:: Language::GetNameForLanguageType (frame_lang));
486
451
}
487
452
488
- // 2. Configure the compiler with a set of default options that are
489
- // appropriate for most situations.
490
- SetupTargetOpts (*m_compiler, *target_sp);
491
-
492
- // 3. Create and install the target on the compiler.
493
- m_compiler->createDiagnostics ();
494
- // Limit the number of error diagnostics we emit.
495
- // A value of 0 means no limit for both LLDB and Clang.
496
- m_compiler->getDiagnostics ().setErrorLimit (target_sp->GetExprErrorLimit ());
497
-
498
- if (auto *target_info = TargetInfo::CreateTargetInfo (
499
- m_compiler->getDiagnostics (),
500
- m_compiler->getInvocation ().TargetOpts )) {
501
- if (log) {
502
- LLDB_LOGF (log, " Target datalayout string: '%s'" ,
503
- target_info->getDataLayoutString ());
504
- LLDB_LOGF (log, " Target ABI: '%s'" , target_info->getABI ().str ().c_str ());
505
- LLDB_LOGF (log, " Target vector alignment: %d" ,
506
- target_info->getMaxVectorAlign ());
507
- }
508
- m_compiler->setTarget (target_info);
509
- } else {
510
- if (log)
511
- LLDB_LOGF (log, " Failed to create TargetInfo for '%s'" ,
512
- m_compiler->getTargetOpts ().Triple .c_str ());
513
-
514
- lldbassert (false && " Failed to create TargetInfo." );
515
- }
516
-
517
- // 4. Set language options.
518
453
lldb::LanguageType language = expr.Language ().AsLanguageType ();
519
- LangOptions &lang_opts = m_compiler-> getLangOpts ();
454
+ LangOptions &lang_opts = compiler. getLangOpts ();
520
455
456
+ // FIXME: should this switch on frame_lang?
521
457
switch (language) {
522
458
case lldb::eLanguageTypeC:
523
459
case lldb::eLanguageTypeC89:
@@ -558,7 +494,7 @@ ClangExpressionParser::ClangExpressionParser(
558
494
case lldb::eLanguageTypeC_plus_plus_11:
559
495
case lldb::eLanguageTypeC_plus_plus_14:
560
496
lang_opts.CPlusPlus11 = true ;
561
- m_compiler-> getHeaderSearchOpts ().UseLibcxx = true ;
497
+ compiler. getHeaderSearchOpts ().UseLibcxx = true ;
562
498
[[fallthrough]];
563
499
case lldb::eLanguageTypeC_plus_plus_03:
564
500
lang_opts.CPlusPlus = true ;
@@ -575,7 +511,7 @@ ClangExpressionParser::ClangExpressionParser(
575
511
lang_opts.ObjC = true ;
576
512
lang_opts.CPlusPlus = true ;
577
513
lang_opts.CPlusPlus11 = true ;
578
- m_compiler-> getHeaderSearchOpts ().UseLibcxx = true ;
514
+ compiler. getHeaderSearchOpts ().UseLibcxx = true ;
579
515
break ;
580
516
}
581
517
@@ -587,42 +523,14 @@ ClangExpressionParser::ClangExpressionParser(
587
523
if (expr.DesiredResultType () == Expression::eResultTypeId)
588
524
lang_opts.DebuggerCastResultToId = true ;
589
525
590
- lang_opts.CharIsSigned = ArchSpec (m_compiler-> getTargetOpts (). Triple . c_str ())
591
- .CharIsSignedByDefault ();
526
+ lang_opts.CharIsSigned =
527
+ ArchSpec (compiler. getTargetOpts (). Triple . c_str ()) .CharIsSignedByDefault ();
592
528
593
529
// Spell checking is a nice feature, but it ends up completing a lot of types
594
530
// that we didn't strictly speaking need to complete. As a result, we spend a
595
531
// long time parsing and importing debug information.
596
532
lang_opts.SpellChecking = false ;
597
533
598
- auto *clang_expr = dyn_cast<ClangUserExpression>(&m_expr);
599
- if (clang_expr && clang_expr->DidImportCxxModules ()) {
600
- LLDB_LOG (log, " Adding lang options for importing C++ modules" );
601
-
602
- lang_opts.Modules = true ;
603
- // We want to implicitly build modules.
604
- lang_opts.ImplicitModules = true ;
605
- // To automatically import all submodules when we import 'std'.
606
- lang_opts.ModulesLocalVisibility = false ;
607
-
608
- // We use the @import statements, so we need this:
609
- // FIXME: We could use the modules-ts, but that currently doesn't work.
610
- lang_opts.ObjC = true ;
611
-
612
- // Options we need to parse libc++ code successfully.
613
- // FIXME: We should ask the driver for the appropriate default flags.
614
- lang_opts.GNUMode = true ;
615
- lang_opts.GNUKeywords = true ;
616
- lang_opts.CPlusPlus11 = true ;
617
- lang_opts.BuiltinHeadersInSystemModules = true ;
618
-
619
- // The Darwin libc expects this macro to be set.
620
- lang_opts.GNUCVersion = 40201 ;
621
-
622
- SetupModuleHeaderPaths (m_compiler.get (), m_include_directories,
623
- target_sp);
624
- }
625
-
626
534
if (process_sp && lang_opts.ObjC ) {
627
535
if (auto *runtime = ObjCLanguageRuntime::Get (*process_sp)) {
628
536
switch (runtime->GetRuntimeVersion ()) {
@@ -651,6 +559,113 @@ ClangExpressionParser::ClangExpressionParser(
651
559
// 'fopen'). Those libc functions are already correctly handled by LLDB, and
652
560
// additionally enabling them as expandable builtins is breaking Clang.
653
561
lang_opts.NoBuiltin = true ;
562
+ }
563
+
564
+ static void SetupImportStdModuleLangOpts (CompilerInstance &compiler) {
565
+ LangOptions &lang_opts = compiler.getLangOpts ();
566
+ lang_opts.Modules = true ;
567
+ // We want to implicitly build modules.
568
+ lang_opts.ImplicitModules = true ;
569
+ // To automatically import all submodules when we import 'std'.
570
+ lang_opts.ModulesLocalVisibility = false ;
571
+
572
+ // We use the @import statements, so we need this:
573
+ // FIXME: We could use the modules-ts, but that currently doesn't work.
574
+ lang_opts.ObjC = true ;
575
+
576
+ // Options we need to parse libc++ code successfully.
577
+ // FIXME: We should ask the driver for the appropriate default flags.
578
+ lang_opts.GNUMode = true ;
579
+ lang_opts.GNUKeywords = true ;
580
+ lang_opts.CPlusPlus11 = true ;
581
+ lang_opts.BuiltinHeadersInSystemModules = true ;
582
+
583
+ // The Darwin libc expects this macro to be set.
584
+ lang_opts.GNUCVersion = 40201 ;
585
+ }
586
+
587
+ // ===----------------------------------------------------------------------===//
588
+ // Implementation of ClangExpressionParser
589
+ // ===----------------------------------------------------------------------===//
590
+
591
+ ClangExpressionParser::ClangExpressionParser (
592
+ ExecutionContextScope *exe_scope, Expression &expr,
593
+ bool generate_debug_info, std::vector<std::string> include_directories,
594
+ std::string filename)
595
+ : ExpressionParser(exe_scope, expr, generate_debug_info), m_compiler(),
596
+ m_pp_callbacks(nullptr ),
597
+ m_include_directories(std::move(include_directories)),
598
+ m_filename(std::move(filename)) {
599
+ Log *log = GetLog (LLDBLog::Expressions);
600
+
601
+ // We can't compile expressions without a target. So if the exe_scope is
602
+ // null or doesn't have a target, then we just need to get out of here. I'll
603
+ // lldbassert and not make any of the compiler objects since
604
+ // I can't return errors directly from the constructor. Further calls will
605
+ // check if the compiler was made and
606
+ // bag out if it wasn't.
607
+
608
+ if (!exe_scope) {
609
+ lldbassert (exe_scope &&
610
+ " Can't make an expression parser with a null scope." );
611
+ return ;
612
+ }
613
+
614
+ lldb::TargetSP target_sp;
615
+ target_sp = exe_scope->CalculateTarget ();
616
+ if (!target_sp) {
617
+ lldbassert (target_sp.get () &&
618
+ " Can't make an expression parser with a null target." );
619
+ return ;
620
+ }
621
+
622
+ // 1. Create a new compiler instance.
623
+ m_compiler = std::make_unique<CompilerInstance>();
624
+
625
+ // Make sure clang uses the same VFS as LLDB.
626
+ m_compiler->createFileManager (FileSystem::Instance ().GetVirtualFileSystem ());
627
+
628
+ std::string abi;
629
+ ArchSpec target_arch;
630
+ target_arch = target_sp->GetArchitecture ();
631
+
632
+ // 2. Configure the compiler with a set of default options that are
633
+ // appropriate for most situations.
634
+ SetupTargetOpts (*m_compiler, *target_sp);
635
+
636
+ // 3. Create and install the target on the compiler.
637
+ m_compiler->createDiagnostics ();
638
+ // Limit the number of error diagnostics we emit.
639
+ // A value of 0 means no limit for both LLDB and Clang.
640
+ m_compiler->getDiagnostics ().setErrorLimit (target_sp->GetExprErrorLimit ());
641
+
642
+ if (auto *target_info = TargetInfo::CreateTargetInfo (
643
+ m_compiler->getDiagnostics (),
644
+ m_compiler->getInvocation ().TargetOpts )) {
645
+ if (log) {
646
+ LLDB_LOGF (log, " Target datalayout string: '%s'" ,
647
+ target_info->getDataLayoutString ());
648
+ LLDB_LOGF (log, " Target ABI: '%s'" , target_info->getABI ().str ().c_str ());
649
+ LLDB_LOGF (log, " Target vector alignment: %d" ,
650
+ target_info->getMaxVectorAlign ());
651
+ }
652
+ m_compiler->setTarget (target_info);
653
+ } else {
654
+ if (log)
655
+ LLDB_LOGF (log, " Failed to create TargetInfo for '%s'" ,
656
+ m_compiler->getTargetOpts ().Triple .c_str ());
657
+
658
+ lldbassert (false && " Failed to create TargetInfo." );
659
+ }
660
+
661
+ // 4. Set language options.
662
+ SetupLangOpts (*m_compiler, *exe_scope, expr);
663
+ if (auto *clang_expr = dyn_cast<ClangUserExpression>(&m_expr);
664
+ clang_expr && clang_expr->DidImportCxxModules ()) {
665
+ LLDB_LOG (log, " Adding lang options for importing C++ modules" );
666
+ SetupImportStdModuleLangOpts (*m_compiler);
667
+ SetupModuleHeaderPaths (m_compiler.get (), m_include_directories, target_sp);
668
+ }
654
669
655
670
// Set CodeGen options
656
671
m_compiler->getCodeGenOpts ().EmitDeclMetadata = true ;
@@ -684,7 +699,7 @@ ClangExpressionParser::ClangExpressionParser(
684
699
m_compiler->createSourceManager (m_compiler->getFileManager ());
685
700
m_compiler->createPreprocessor (TU_Complete);
686
701
687
- switch (language ) {
702
+ switch (expr. Language (). AsLanguageType () ) {
688
703
case lldb::eLanguageTypeC:
689
704
case lldb::eLanguageTypeC89:
690
705
case lldb::eLanguageTypeC99:
0 commit comments