[Lldb-commits] [lldb] [lldb][ClangExpressionParser][NFC] Factor LangOptions logic out of ClangExpressionParser constructor (PR #101669)

Adrian Prantl via lldb-commits lldb-commits at lists.llvm.org
Fri Aug 2 08:35:38 PDT 2024


================
@@ -615,6 +484,147 @@ 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>();
----------------
adrian-prantl wrote:

It seems unnecessary for this to be unique_ptr, but I assume this is the `IsValid` flag for the object?
It would be cleaner if we had a createClangExpressionParser static function that returned an Expected<ClangExpressionParser>.

Not a big deal though.

https://github.com/llvm/llvm-project/pull/101669


More information about the lldb-commits mailing list