[Lldb-commits] [PATCH] D144929: Add SBCommandInterpreter::UserCommandExists

Jim Ingham via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Mon Feb 27 15:36:38 PST 2023


jingham created this revision.
jingham added reviewers: JDevlieghere, mib.
Herald added a project: All.
jingham requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

Turns out there's no API to find out if a user command exists.  There's SBCommandInterpreter::CommandExists but that only checks for built-in commands.  I needed this to write a test for an upcoming patch, and it's a useful thing to know.  I added docs so it's clear CommandExists is for builtin commands and this one is for `command script add` commands.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D144929

Files:
  lldb/include/lldb/API/SBCommandInterpreter.h
  lldb/source/API/SBCommandInterpreter.cpp
  lldb/test/API/commands/command/script/TestCommandScript.py


Index: lldb/test/API/commands/command/script/TestCommandScript.py
===================================================================
--- lldb/test/API/commands/command/script/TestCommandScript.py
+++ lldb/test/API/commands/command/script/TestCommandScript.py
@@ -19,6 +19,11 @@
     def pycmd_tests(self):
         self.runCmd("command source py_import")
 
+        # Test that we did indeed add these commands as user commands:
+        interp = self.dbg.GetCommandInterpreter()
+        self.expectTrue(interp.UserCommandExists("foobar"), "foobar exists")
+        self.expectFalse(interp.CommandExists("foobar"), "It is not a builtin.")
+
         # Test a bunch of different kinds of python callables with
         # both 4 and 5 positional arguments.
         self.expect("foobar", substrs=["All good"])
Index: lldb/source/API/SBCommandInterpreter.cpp
===================================================================
--- lldb/source/API/SBCommandInterpreter.cpp
+++ lldb/source/API/SBCommandInterpreter.cpp
@@ -118,6 +118,13 @@
                                           : false);
 }
 
+bool SBCommandInterpreter::UserCommandExists(const char *cmd) {
+  LLDB_INSTRUMENT_VA(this, cmd);
+
+  return (((cmd != nullptr) && IsValid()) ? m_opaque_ptr->UserCommandExists(cmd)
+                                          : false);
+}
+
 bool SBCommandInterpreter::AliasExists(const char *cmd) {
   LLDB_INSTRUMENT_VA(this, cmd);
 
Index: lldb/include/lldb/API/SBCommandInterpreter.h
===================================================================
--- lldb/include/lldb/API/SBCommandInterpreter.h
+++ lldb/include/lldb/API/SBCommandInterpreter.h
@@ -45,8 +45,34 @@
 
   bool IsValid() const;
 
+  /// Return whether a built-in command with the passed in
+  /// name or command path exists.
+  ///
+  /// \param[in] cmd
+  ///   The command or command path to search for.
+  ///
+  /// \return
+  ///   \b true if the command exists, \b false otherwise.
   bool CommandExists(const char *cmd);
 
+  /// Return whether a user defined command with the passed in
+  /// name or command path exists.
+  ///
+  /// \param[in] cmd
+  ///   The command or command path to search for.
+  ///
+  /// \return
+  ///   \b true if the command exists, \b false otherwise.
+  bool UserCommandExists(const char *cmd);
+
+  /// Return whether the passed in name or command path
+  /// exists and is an alias to some other command.
+  ///
+  /// \param[in] cmd
+  ///   The command or command path to search for.
+  ///
+  /// \return
+  ///   \b true if the command exists, \b false otherwise.
   bool AliasExists(const char *cmd);
 
   lldb::SBBroadcaster GetBroadcaster();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144929.500932.patch
Type: text/x-patch
Size: 2661 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20230227/20071988/attachment-0001.bin>


More information about the lldb-commits mailing list