Author: Michael Buch Date: 2024-08-02T22:27:40+01:00 New Revision: 12e3a06cb7615fbd91031420f3dec2a85d7877d6
URL: https://github.com/llvm/llvm-project/commit/12e3a06cb7615fbd91031420f3dec2a85d7877d6 DIFF: https://github.com/llvm/llvm-project/commit/12e3a06cb7615fbd91031420f3dec2a85d7877d6.diff LOG: [lldb][ClangExpressionParser][NFC] Factor LangOptions logic out of ClangExpressionParser constructor (#101669) We plan to eventually use the Clang driver to initialize the `CompilerInstance`. This should make refactorings of this code more straightforward. **Changes**: * Introduced `SetupLangOpts` and `SetupImportStdModuleLangOpts` * Called them from `ClangExpressionParser::ClangExpressionParser` Added: Modified: lldb/include/lldb/Expression/Expression.h lldb/include/lldb/Expression/UserExpression.h lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp Removed: ################################################################################ diff --git a/lldb/include/lldb/Expression/Expression.h b/lldb/include/lldb/Expression/Expression.h index 356fe4b82ae43..8de9364436ccf 100644 --- a/lldb/include/lldb/Expression/Expression.h +++ b/lldb/include/lldb/Expression/Expression.h @@ -56,7 +56,7 @@ class Expression { /// Return the desired result type of the function, or eResultTypeAny if /// in diff erent. - virtual ResultType DesiredResultType() { return eResultTypeAny; } + virtual ResultType DesiredResultType() const { return eResultTypeAny; } /// Flags diff --git a/lldb/include/lldb/Expression/UserExpression.h b/lldb/include/lldb/Expression/UserExpression.h index b04d00b72e8fa..7ce463d2cb4e7 100644 --- a/lldb/include/lldb/Expression/UserExpression.h +++ b/lldb/include/lldb/Expression/UserExpression.h @@ -206,7 +206,7 @@ class UserExpression : public Expression { /// Return the desired result type of the function, or eResultTypeAny if /// in diff erent. - ResultType DesiredResultType() override { return m_desired_type; } + ResultType DesiredResultType() const override { return m_desired_type; } /// Return true if validation code should be inserted into the expression. bool NeedsValidation() override { return true; } diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp index 2bd58d568cc6d..2a8bdf29314e4 100644 --- a/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp +++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp @@ -77,6 +77,7 @@ #include "lldb/Host/HostInfo.h" #include "lldb/Symbol/SymbolVendor.h" #include "lldb/Target/ExecutionContext.h" +#include "lldb/Target/ExecutionContextScope.h" #include "lldb/Target/Language.h" #include "lldb/Target/Process.h" #include "lldb/Target/Target.h" @@ -425,56 +426,20 @@ static void SetupTargetOpts(CompilerInstance &compiler, compiler.getTargetOpts().ABI = std::move(abi); } -//===----------------------------------------------------------------------===// -// Implementation of ClangExpressionParser -//===----------------------------------------------------------------------===// - -ClangExpressionParser::ClangExpressionParser( - ExecutionContextScope *exe_scope, Expression &expr, - bool generate_debug_info, std::vector<std::string> include_directories, - std::string filename) - : ExpressionParser(exe_scope, expr, generate_debug_info), m_compiler(), - m_pp_callbacks(nullptr), - m_include_directories(std::move(include_directories)), - m_filename(std::move(filename)) { +static void SetupLangOpts(CompilerInstance &compiler, + ExecutionContextScope &exe_scope, + const Expression &expr) { Log *log = GetLog(LLDBLog::Expressions); - // We can't compile expressions without a target. So if the exe_scope is - // null or doesn't have a target, then we just need to get out of here. I'll - // lldbassert and not make any of the compiler objects since - // I can't return errors directly from the constructor. Further calls will - // check if the compiler was made and - // bag out if it wasn't. - - if (!exe_scope) { - lldbassert(exe_scope && - "Can't make an expression parser with a null scope."); - return; - } - - lldb::TargetSP target_sp; - target_sp = exe_scope->CalculateTarget(); - if (!target_sp) { - lldbassert(target_sp.get() && - "Can't make an expression parser with a null target."); - return; - } - - // 1. Create a new compiler instance. - m_compiler = std::make_unique<CompilerInstance>(); + // If the expression is being evaluated in the context of an existing stack + // frame, we introspect to see if the language runtime is available. - // Make sure clang uses the same VFS as LLDB. - m_compiler->createFileManager(FileSystem::Instance().GetVirtualFileSystem()); + lldb::StackFrameSP frame_sp = exe_scope.CalculateStackFrame(); + lldb::ProcessSP process_sp = exe_scope.CalculateProcess(); // Defaults to lldb::eLanguageTypeUnknown. lldb::LanguageType frame_lang = expr.Language().AsLanguageType(); - // If the expression is being evaluated in the context of an existing stack - // frame, we introspect to see if the language runtime is available. - - lldb::StackFrameSP frame_sp = exe_scope->CalculateStackFrame(); - lldb::ProcessSP process_sp = exe_scope->CalculateProcess(); - // Make sure the user hasn't provided a preferred execution language with // `expression --language X -- ...` if (frame_sp && frame_lang == lldb::eLanguageTypeUnknown) @@ -482,42 +447,13 @@ ClangExpressionParser::ClangExpressionParser( if (process_sp && frame_lang != lldb::eLanguageTypeUnknown) { LLDB_LOGF(log, "Frame has language of type %s", - Language::GetNameForLanguageType(frame_lang)); + lldb_private::Language::GetNameForLanguageType(frame_lang)); } - // 2. Configure the compiler with a set of default options that are - // appropriate for most situations. - SetupTargetOpts(*m_compiler, *target_sp); - - // 3. Create and install the target on the compiler. - m_compiler->createDiagnostics(); - // Limit the number of error diagnostics we emit. - // A value of 0 means no limit for both LLDB and Clang. - m_compiler->getDiagnostics().setErrorLimit(target_sp->GetExprErrorLimit()); - - if (auto *target_info = TargetInfo::CreateTargetInfo( - m_compiler->getDiagnostics(), - m_compiler->getInvocation().TargetOpts)) { - if (log) { - LLDB_LOGF(log, "Target datalayout string: '%s'", - target_info->getDataLayoutString()); - LLDB_LOGF(log, "Target ABI: '%s'", target_info->getABI().str().c_str()); - LLDB_LOGF(log, "Target vector alignment: %d", - target_info->getMaxVectorAlign()); - } - m_compiler->setTarget(target_info); - } else { - if (log) - LLDB_LOGF(log, "Failed to create TargetInfo for '%s'", - m_compiler->getTargetOpts().Triple.c_str()); - - lldbassert(false && "Failed to create TargetInfo."); - } - - // 4. Set language options. lldb::LanguageType language = expr.Language().AsLanguageType(); - LangOptions &lang_opts = m_compiler->getLangOpts(); + LangOptions &lang_opts = compiler.getLangOpts(); + // FIXME: should this switch on frame_lang? switch (language) { case lldb::eLanguageTypeC: case lldb::eLanguageTypeC89: @@ -558,7 +494,7 @@ ClangExpressionParser::ClangExpressionParser( case lldb::eLanguageTypeC_plus_plus_11: case lldb::eLanguageTypeC_plus_plus_14: lang_opts.CPlusPlus11 = true; - m_compiler->getHeaderSearchOpts().UseLibcxx = true; + compiler.getHeaderSearchOpts().UseLibcxx = true; [[fallthrough]]; case lldb::eLanguageTypeC_plus_plus_03: lang_opts.CPlusPlus = true; @@ -575,7 +511,7 @@ ClangExpressionParser::ClangExpressionParser( lang_opts.ObjC = true; lang_opts.CPlusPlus = true; lang_opts.CPlusPlus11 = true; - m_compiler->getHeaderSearchOpts().UseLibcxx = true; + compiler.getHeaderSearchOpts().UseLibcxx = true; break; } @@ -587,42 +523,14 @@ ClangExpressionParser::ClangExpressionParser( if (expr.DesiredResultType() == Expression::eResultTypeId) lang_opts.DebuggerCastResultToId = true; - lang_opts.CharIsSigned = ArchSpec(m_compiler->getTargetOpts().Triple.c_str()) - .CharIsSignedByDefault(); + lang_opts.CharIsSigned = + ArchSpec(compiler.getTargetOpts().Triple.c_str()).CharIsSignedByDefault(); // Spell checking is a nice feature, but it ends up completing a lot of types // that we didn't strictly speaking need to complete. As a result, we spend a // long time parsing and importing debug information. lang_opts.SpellChecking = false; - auto *clang_expr = dyn_cast<ClangUserExpression>(&m_expr); - if (clang_expr && clang_expr->DidImportCxxModules()) { - LLDB_LOG(log, "Adding lang options for importing C++ modules"); - - lang_opts.Modules = true; - // We want to implicitly build modules. - lang_opts.ImplicitModules = true; - // To automatically import all submodules when we import 'std'. - lang_opts.ModulesLocalVisibility = false; - - // We use the @import statements, so we need this: - // FIXME: We could use the modules-ts, but that currently doesn't work. - lang_opts.ObjC = true; - - // Options we need to parse libc++ code successfully. - // FIXME: We should ask the driver for the appropriate default flags. - lang_opts.GNUMode = true; - lang_opts.GNUKeywords = true; - lang_opts.CPlusPlus11 = true; - lang_opts.BuiltinHeadersInSystemModules = true; - - // The Darwin libc expects this macro to be set. - lang_opts.GNUCVersion = 40201; - - SetupModuleHeaderPaths(m_compiler.get(), m_include_directories, - target_sp); - } - if (process_sp && lang_opts.ObjC) { if (auto *runtime = ObjCLanguageRuntime::Get(*process_sp)) { switch (runtime->GetRuntimeVersion()) { @@ -651,6 +559,113 @@ ClangExpressionParser::ClangExpressionParser( // 'fopen'). Those libc functions are already correctly handled by LLDB, and // additionally enabling them as expandable builtins is breaking Clang. lang_opts.NoBuiltin = true; +} + +static void SetupImportStdModuleLangOpts(CompilerInstance &compiler) { + LangOptions &lang_opts = compiler.getLangOpts(); + lang_opts.Modules = true; + // We want to implicitly build modules. + lang_opts.ImplicitModules = true; + // To automatically import all submodules when we import 'std'. + lang_opts.ModulesLocalVisibility = false; + + // We use the @import statements, so we need this: + // FIXME: We could use the modules-ts, but that currently doesn't work. + lang_opts.ObjC = true; + + // Options we need to parse libc++ code successfully. + // FIXME: We should ask the driver for the appropriate default flags. + lang_opts.GNUMode = true; + lang_opts.GNUKeywords = true; + lang_opts.CPlusPlus11 = true; + lang_opts.BuiltinHeadersInSystemModules = true; + + // The Darwin libc expects this macro to be set. + lang_opts.GNUCVersion = 40201; +} + +//===----------------------------------------------------------------------===// +// Implementation of ClangExpressionParser +//===----------------------------------------------------------------------===// + +ClangExpressionParser::ClangExpressionParser( + ExecutionContextScope *exe_scope, Expression &expr, + bool generate_debug_info, std::vector<std::string> include_directories, + std::string filename) + : ExpressionParser(exe_scope, expr, generate_debug_info), m_compiler(), + m_pp_callbacks(nullptr), + m_include_directories(std::move(include_directories)), + m_filename(std::move(filename)) { + Log *log = GetLog(LLDBLog::Expressions); + + // We can't compile expressions without a target. So if the exe_scope is + // null or doesn't have a target, then we just need to get out of here. I'll + // lldbassert and not make any of the compiler objects since + // I can't return errors directly from the constructor. Further calls will + // check if the compiler was made and + // bag out if it wasn't. + + if (!exe_scope) { + lldbassert(exe_scope && + "Can't make an expression parser with a null scope."); + return; + } + + lldb::TargetSP target_sp; + target_sp = exe_scope->CalculateTarget(); + if (!target_sp) { + lldbassert(target_sp.get() && + "Can't make an expression parser with a null target."); + return; + } + + // 1. Create a new compiler instance. + m_compiler = std::make_unique<CompilerInstance>(); + + // Make sure clang uses the same VFS as LLDB. + m_compiler->createFileManager(FileSystem::Instance().GetVirtualFileSystem()); + + std::string abi; + ArchSpec target_arch; + target_arch = target_sp->GetArchitecture(); + + // 2. Configure the compiler with a set of default options that are + // appropriate for most situations. + SetupTargetOpts(*m_compiler, *target_sp); + + // 3. Create and install the target on the compiler. + m_compiler->createDiagnostics(); + // Limit the number of error diagnostics we emit. + // A value of 0 means no limit for both LLDB and Clang. + m_compiler->getDiagnostics().setErrorLimit(target_sp->GetExprErrorLimit()); + + if (auto *target_info = TargetInfo::CreateTargetInfo( + m_compiler->getDiagnostics(), + m_compiler->getInvocation().TargetOpts)) { + if (log) { + LLDB_LOGF(log, "Target datalayout string: '%s'", + target_info->getDataLayoutString()); + LLDB_LOGF(log, "Target ABI: '%s'", target_info->getABI().str().c_str()); + LLDB_LOGF(log, "Target vector alignment: %d", + target_info->getMaxVectorAlign()); + } + m_compiler->setTarget(target_info); + } else { + if (log) + LLDB_LOGF(log, "Failed to create TargetInfo for '%s'", + m_compiler->getTargetOpts().Triple.c_str()); + + lldbassert(false && "Failed to create TargetInfo."); + } + + // 4. Set language options. + SetupLangOpts(*m_compiler, *exe_scope, expr); + if (auto *clang_expr = dyn_cast<ClangUserExpression>(&m_expr); + clang_expr && clang_expr->DidImportCxxModules()) { + LLDB_LOG(log, "Adding lang options for importing C++ modules"); + SetupImportStdModuleLangOpts(*m_compiler); + SetupModuleHeaderPaths(m_compiler.get(), m_include_directories, target_sp); + } // Set CodeGen options m_compiler->getCodeGenOpts().EmitDeclMetadata = true; @@ -684,7 +699,7 @@ ClangExpressionParser::ClangExpressionParser( m_compiler->createSourceManager(m_compiler->getFileManager()); m_compiler->createPreprocessor(TU_Complete); - switch (language) { + switch (expr.Language().AsLanguageType()) { case lldb::eLanguageTypeC: case lldb::eLanguageTypeC89: case lldb::eLanguageTypeC99: _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits