[Lldb-commits] [lldb] d616a6b - [lldb] Fix that the expression commands --top-level flag overwrites --allow-jit false

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Thu Apr 22 09:51:25 PDT 2021


Author: Raphael Isemann
Date: 2021-04-22T18:51:03+02:00
New Revision: d616a6bd107fcc0dc74f79e174e0b4fe27b26fe6

URL: https://github.com/llvm/llvm-project/commit/d616a6bd107fcc0dc74f79e174e0b4fe27b26fe6
DIFF: https://github.com/llvm/llvm-project/commit/d616a6bd107fcc0dc74f79e174e0b4fe27b26fe6.diff

LOG: [lldb] Fix that the expression commands --top-level flag overwrites --allow-jit false

The `--allow-jit` flag allows the user to force the IR interpreter to run the
provided expression.

The `--top-level` flag parses and injects the code as if its in the top level
scope of a source file.

Both flags just change the ExecutionPolicy of the expression:
* `--allow-jit true` -> doesn't change anything (its the default)
* `--allow-jit false` -> ExecutionPolicyNever
* `--top-level` -> ExecutionPolicyTopLevel

Passing `--allow-jit false` and `--top-level` currently causes the `--top-level`
to silently overwrite the ExecutionPolicy value that was set by `--allow-jit
false`. There isn't any ExecutionPolicy value that says "top-level but only
interpret", so I would say we reject this combination of flags until someone
finds time to refactor top-level feature out of the ExecutionPolicy enum.

The SBExpressionOptions suffer from a similar symptom as `SetTopLevel` and
`SetAllowJIT` just silently disable each other. But those functions don't have
any error handling, so not a lot we can do about this in the meantime.

Reviewed By: labath, kastiglione

Differential Revision: https://reviews.llvm.org/D91780

Added: 
    

Modified: 
    lldb/source/Commands/CommandObjectExpression.cpp
    lldb/test/API/commands/expression/dont_allow_jit/TestAllowJIT.py

Removed: 
    


################################################################################
diff  --git a/lldb/source/Commands/CommandObjectExpression.cpp b/lldb/source/Commands/CommandObjectExpression.cpp
index c7866f966569f..4f4318edc14fe 100644
--- a/lldb/source/Commands/CommandObjectExpression.cpp
+++ b/lldb/source/Commands/CommandObjectExpression.cpp
@@ -408,6 +408,13 @@ bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr,
   lldb::ValueObjectSP result_valobj_sp;
   StackFrame *frame = exe_ctx.GetFramePtr();
 
+  if (m_command_options.top_level && !m_command_options.allow_jit) {
+    result.AppendErrorWithFormat(
+        "Can't disable JIT compilation for top-level expressions.\n");
+    result.SetStatus(eReturnStatusFailed);
+    return false;
+  }
+
   const EvaluateExpressionOptions options = GetEvalOptions(target);
   ExpressionResults success = target.EvaluateExpression(
       expr, frame, result_valobj_sp, options, &m_fixed_expression);

diff  --git a/lldb/test/API/commands/expression/dont_allow_jit/TestAllowJIT.py b/lldb/test/API/commands/expression/dont_allow_jit/TestAllowJIT.py
index ab1438479aab9..7bba9c8d2c8d7 100644
--- a/lldb/test/API/commands/expression/dont_allow_jit/TestAllowJIT.py
+++ b/lldb/test/API/commands/expression/dont_allow_jit/TestAllowJIT.py
@@ -80,3 +80,16 @@ def expr_options_test(self):
         self.assertSuccess(result.GetError())
         self.assertEqual(result.GetValueAsSigned(), 18, "got the right value.")
 
+    def test_allow_jit_with_top_level(self):
+        """Test combined --allow-jit and --top-level flags"""
+        # Can't force interpreting for top-level expressions which are always
+        # injected.
+        self.expect("expr --allow-jit false --top-level -- int i;", error=True,
+                    substrs=["Can't disable JIT compilation for top-level expressions."])
+
+        self.build()
+        lldbutil.run_to_source_breakpoint(self, "Set a breakpoint here", lldb.SBFileSpec("main.c"))
+        # Allowing JITing for top-level expressions is redundant but should work.
+        self.expect("expr --allow-jit true --top-level -- int top_level_f() { return 2; }")
+        # Make sure we actually declared a working top-level function.
+        self.expect_expr("top_level_f()", result_value="2")


        


More information about the lldb-commits mailing list