Add SBCommandInterpreter::UserCommandExists parallel to CommandExists.
authorJim Ingham <jingham@apple.com>
Tue, 28 Feb 2023 23:58:14 +0000 (15:58 -0800)
committerJim Ingham <jingham@apple.com>
Tue, 28 Feb 2023 23:58:14 +0000 (15:58 -0800)
The latter only checks built-in commands.  I also added some docs to
make the distinction clear and a test.

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

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

index 0830145..08ed989 100644 (file)
@@ -45,8 +45,34 @@ public:
 
   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();
index cd6ef3b..35c5528 100644 (file)
@@ -118,6 +118,13 @@ bool SBCommandInterpreter::CommandExists(const char *cmd) {
                                           : 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 7e8206c..b81acef 100644 (file)
@@ -19,6 +19,11 @@ class CmdPythonTestCase(TestBase):
     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"])