Skip to content

[lldb-dap] Correctly detect alias commands with arguments in repl #92137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 17, 2024

Conversation

labath
Copy link
Collaborator

@labath labath commented May 14, 2024

ResolveCommand will not succeed for an alias command with arguments, and the code wasn't providing any. Replace that with explicit query(ies) for the existence of a command with the given name.

ResolveCommand will not succeed for an alias command with arguments, and
the code wasn't providing any. Replace that with explicit query(ies) for
the existence of a command with the given name.
@llvmbot
Copy link
Member

llvmbot commented May 14, 2024

@llvm/pr-subscribers-lldb

Author: Pavel Labath (labath)

Changes

ResolveCommand will not succeed for an alias command with arguments, and the code wasn't providing any. Replace that with explicit query(ies) for the existence of a command with the given name.


Full diff: https://github.com/llvm/llvm-project/pull/92137.diff

4 Files Affected:

  • (added) lldb/test/API/tools/lldb-dap/repl-mode/Makefile (+3)
  • (added) lldb/test/API/tools/lldb-dap/repl-mode/TestDAP_repl_mode_detection.py (+56)
  • (added) lldb/test/API/tools/lldb-dap/repl-mode/main.cpp (+15)
  • (modified) lldb/tools/lldb-dap/DAP.cpp (+5-3)
diff --git a/lldb/test/API/tools/lldb-dap/repl-mode/Makefile b/lldb/test/API/tools/lldb-dap/repl-mode/Makefile
new file mode 100644
index 0000000000000..99998b20bcb05
--- /dev/null
+++ b/lldb/test/API/tools/lldb-dap/repl-mode/Makefile
@@ -0,0 +1,3 @@
+CXX_SOURCES := main.cpp
+
+include Makefile.rules
diff --git a/lldb/test/API/tools/lldb-dap/repl-mode/TestDAP_repl_mode_detection.py b/lldb/test/API/tools/lldb-dap/repl-mode/TestDAP_repl_mode_detection.py
new file mode 100644
index 0000000000000..8beecac26160e
--- /dev/null
+++ b/lldb/test/API/tools/lldb-dap/repl-mode/TestDAP_repl_mode_detection.py
@@ -0,0 +1,56 @@
+"""
+Test lldb-dap repl mode detection
+"""
+
+import lldbdap_testcase
+import dap_server
+from lldbsuite.test import lldbutil
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+
+
+class TestDAP_repl_mode_detection(lldbdap_testcase.DAPTestCaseBase):
+
+    def assertEvaluate(self, expression, regex):
+        self.assertRegex(
+            self.dap_server.request_evaluate(expression, context="repl")["body"][
+                "result"
+            ],
+            regex,
+        )
+
+    def test_completions(self):
+        program = self.getBuildArtifact("a.out")
+        self.build_and_launch(program)
+
+        source = "main.cpp"
+        breakpoint1_line = line_number(source, "// breakpoint 1")
+        breakpoint2_line = line_number(source, "// breakpoint 2")
+
+        self.set_source_breakpoints(source, [breakpoint1_line, breakpoint2_line])
+
+        self.assertEvaluate(
+            "`command regex user_command s/^$/platform/", r"\(lldb\) command regex"
+        )
+        self.assertEvaluate(
+            "`command alias alias_command platform", r"\(lldb\) command alias"
+        )
+        self.assertEvaluate(
+            "`command alias alias_command_with_arg platform select --sysroot %1 remote-linux",
+            r"\(lldb\) command alias",
+        )
+
+        self.continue_to_next_stop()
+        self.assertEvaluate("user_command", "474747")
+        self.assertEvaluate("alias_command", "474747")
+        self.assertEvaluate("alias_command_with_arg", "474747")
+        self.assertEvaluate("platform", "474747")
+
+        self.continue_to_next_stop()
+        platform_help_needle = "Commands to manage and create platforms"
+        self.assertEvaluate("user_command", platform_help_needle)
+        self.assertEvaluate("alias_command", platform_help_needle)
+        self.assertEvaluate(
+            "alias_command_with_arg " + self.getBuildDir(), "Platform: remote-linux"
+        )
+        self.assertEvaluate("platform", platform_help_needle)
diff --git a/lldb/test/API/tools/lldb-dap/repl-mode/main.cpp b/lldb/test/API/tools/lldb-dap/repl-mode/main.cpp
new file mode 100644
index 0000000000000..52561d3471abf
--- /dev/null
+++ b/lldb/test/API/tools/lldb-dap/repl-mode/main.cpp
@@ -0,0 +1,15 @@
+void noop() {}
+
+void fun() {
+  int user_command = 474747;
+  int alias_command = 474747;
+  int alias_command_with_arg = 474747;
+  int platform = 474747; // built-in command
+  noop();                // breakpoint 1
+}
+
+int main() {
+  fun();
+  noop(); // breakpoint 2
+  return 0;
+}
diff --git a/lldb/tools/lldb-dap/DAP.cpp b/lldb/tools/lldb-dap/DAP.cpp
index 55ff1493c1011..c7eb3db4304a9 100644
--- a/lldb/tools/lldb-dap/DAP.cpp
+++ b/lldb/tools/lldb-dap/DAP.cpp
@@ -14,6 +14,7 @@
 
 #include "DAP.h"
 #include "LLDBUtils.h"
+#include "lldb/API/SBCommandInterpreter.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/FormatVariadic.h"
 
@@ -405,9 +406,10 @@ ExpressionContext DAP::DetectExpressionContext(lldb::SBFrame frame,
     std::pair<llvm::StringRef, llvm::StringRef> token =
         llvm::getToken(expression);
     std::string term = token.first.str();
-    lldb::SBCommandReturnObject result;
-    debugger.GetCommandInterpreter().ResolveCommand(term.c_str(), result);
-    bool term_is_command = result.Succeeded();
+    lldb::SBCommandInterpreter interpreter = debugger.GetCommandInterpreter();
+    bool term_is_command = interpreter.CommandExists(term.c_str()) ||
+                           interpreter.UserCommandExists(term.c_str()) ||
+                           interpreter.AliasExists(term.c_str());
     bool term_is_variable = frame.FindVariable(term.c_str()).IsValid();
 
     // If we have both a variable and command, warn the user about the conflict.

Copy link

github-actions bot commented May 14, 2024

✅ With the latest revision this PR passed the Python code formatter.

@labath labath merged commit 33bf08e into llvm:main May 17, 2024
@labath labath deleted the alias branch May 17, 2024 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants