Get rid of the C-string parameter in DoExecute

Summary:
This patch gets rid of the C-string parameter in the RawCommandObject::DoExecute function,
making the code simpler and less memory unsafe.

There seems to be a assumption in some command objects that this parameter could be a nullptr,
but from what I can see the rest of the API doesn't actually allow this (and other command
objects and related code pieces dereference this parameter without any checks).

Especially CommandObjectRegexCommand has error handling code for a nullptr that is now gone.

Reviewers: davide, jingham, teemperor

Reviewed By: teemperor

Subscribers: jingham, lldb-commits

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

llvm-svn: 336955
diff --git a/lldb/source/Interpreter/CommandObjectRegexCommand.cpp b/lldb/source/Interpreter/CommandObjectRegexCommand.cpp
index 6826a2c..a5362c3 100644
--- a/lldb/source/Interpreter/CommandObjectRegexCommand.cpp
+++ b/lldb/source/Interpreter/CommandObjectRegexCommand.cpp
@@ -35,53 +35,47 @@
 //----------------------------------------------------------------------
 CommandObjectRegexCommand::~CommandObjectRegexCommand() {}
 
-bool CommandObjectRegexCommand::DoExecute(const char *command,
+bool CommandObjectRegexCommand::DoExecute(llvm::StringRef command,
                                           CommandReturnObject &result) {
-  if (command) {
-    EntryCollection::const_iterator pos, end = m_entries.end();
-    for (pos = m_entries.begin(); pos != end; ++pos) {
-      RegularExpression::Match regex_match(m_max_matches);
+  EntryCollection::const_iterator pos, end = m_entries.end();
+  for (pos = m_entries.begin(); pos != end; ++pos) {
+    RegularExpression::Match regex_match(m_max_matches);
 
-      if (pos->regex.Execute(command, &regex_match)) {
-        std::string new_command(pos->command);
-        std::string match_str;
-        char percent_var[8];
-        size_t idx, percent_var_idx;
-        for (uint32_t match_idx = 1; match_idx <= m_max_matches; ++match_idx) {
-          if (regex_match.GetMatchAtIndex(command, match_idx, match_str)) {
-            const int percent_var_len =
-                ::snprintf(percent_var, sizeof(percent_var), "%%%u", match_idx);
-            for (idx = 0; (percent_var_idx = new_command.find(
-                               percent_var, idx)) != std::string::npos;) {
-              new_command.erase(percent_var_idx, percent_var_len);
-              new_command.insert(percent_var_idx, match_str);
-              idx += percent_var_idx + match_str.size();
-            }
+    if (pos->regex.Execute(command, &regex_match)) {
+      std::string new_command(pos->command);
+      std::string match_str;
+      char percent_var[8];
+      size_t idx, percent_var_idx;
+      for (uint32_t match_idx = 1; match_idx <= m_max_matches; ++match_idx) {
+        if (regex_match.GetMatchAtIndex(command, match_idx, match_str)) {
+          const int percent_var_len =
+              ::snprintf(percent_var, sizeof(percent_var), "%%%u", match_idx);
+          for (idx = 0; (percent_var_idx = new_command.find(
+                             percent_var, idx)) != std::string::npos;) {
+            new_command.erase(percent_var_idx, percent_var_len);
+            new_command.insert(percent_var_idx, match_str);
+            idx += percent_var_idx + match_str.size();
           }
         }
-        // Interpret the new command and return this as the result!
-        if (m_interpreter.GetExpandRegexAliases())
-          result.GetOutputStream().Printf("%s\n", new_command.c_str());
-        // Pass in true for "no context switching".  The command that called us
-        // should have set up the context appropriately, we shouldn't have to
-        // redo that.
-        return m_interpreter.HandleCommand(new_command.c_str(),
-                                           eLazyBoolCalculate, result, nullptr,
-                                           true, true);
       }
+      // Interpret the new command and return this as the result!
+      if (m_interpreter.GetExpandRegexAliases())
+        result.GetOutputStream().Printf("%s\n", new_command.c_str());
+      // Pass in true for "no context switching".  The command that called us
+      // should have set up the context appropriately, we shouldn't have to
+      // redo that.
+      return m_interpreter.HandleCommand(
+          new_command.c_str(), eLazyBoolCalculate, result, nullptr, true, true);
     }
-    result.SetStatus(eReturnStatusFailed);
-    if (!GetSyntax().empty())
-      result.AppendError(GetSyntax());
-    else
-      result.AppendErrorWithFormat("Command contents '%s' failed to match any "
-                                   "regular expression in the '%s' regex "
-                                   "command.\n",
-                                   command, m_cmd_name.c_str());
-    return false;
   }
-  result.AppendError("empty command passed to regular expression command");
   result.SetStatus(eReturnStatusFailed);
+  if (!GetSyntax().empty())
+    result.AppendError(GetSyntax());
+  else
+    result.GetOutputStream() << "Command contents '" << command
+                             << "' failed to match any "
+                                "regular expression in the '"
+                             << m_cmd_name << "' regex ";
   return false;
 }
 
diff --git a/lldb/source/Interpreter/CommandObjectScript.cpp b/lldb/source/Interpreter/CommandObjectScript.cpp
index dfb1ba5..fa1516d 100644
--- a/lldb/source/Interpreter/CommandObjectScript.cpp
+++ b/lldb/source/Interpreter/CommandObjectScript.cpp
@@ -40,7 +40,7 @@
 
 CommandObjectScript::~CommandObjectScript() {}
 
-bool CommandObjectScript::DoExecute(const char *command,
+bool CommandObjectScript::DoExecute(llvm::StringRef command,
                                     CommandReturnObject &result) {
 #ifdef LLDB_DISABLE_PYTHON
   // if we ever support languages other than Python this simple #ifdef won't
@@ -69,7 +69,7 @@
                                     // for formatting.. make sure we keep up to
                                     // date with it
 
-  if (command == nullptr || command[0] == '\0') {
+  if (command.empty()) {
     script_interpreter->ExecuteInterpreterLoop();
     result.SetStatus(eReturnStatusSuccessFinishNoResult);
     return result.Succeeded();
diff --git a/lldb/source/Interpreter/CommandObjectScript.h b/lldb/source/Interpreter/CommandObjectScript.h
index 2c05ca9..7a61b06 100644
--- a/lldb/source/Interpreter/CommandObjectScript.h
+++ b/lldb/source/Interpreter/CommandObjectScript.h
@@ -30,7 +30,7 @@
   ~CommandObjectScript() override;
 
 protected:
-  bool DoExecute(const char *command, CommandReturnObject &result) override;
+  bool DoExecute(llvm::StringRef command, CommandReturnObject &result) override;
 };
 
 } // namespace lldb_private