[Lldb-commits] [lldb] r336955 - Get rid of the C-string parameter in DoExecute

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Thu Jul 12 15:28:52 PDT 2018


Author: teemperor
Date: Thu Jul 12 15:28:52 2018
New Revision: 336955

URL: http://llvm.org/viewvc/llvm-project?rev=336955&view=rev
Log:
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

Modified:
    lldb/trunk/include/lldb/Interpreter/CommandObject.h
    lldb/trunk/include/lldb/Interpreter/CommandObjectRegexCommand.h
    lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h
    lldb/trunk/source/Commands/CommandObjectCommands.cpp
    lldb/trunk/source/Commands/CommandObjectExpression.cpp
    lldb/trunk/source/Commands/CommandObjectExpression.h
    lldb/trunk/source/Commands/CommandObjectPlatform.cpp
    lldb/trunk/source/Commands/CommandObjectSettings.cpp
    lldb/trunk/source/Commands/CommandObjectThread.cpp
    lldb/trunk/source/Commands/CommandObjectType.cpp
    lldb/trunk/source/Commands/CommandObjectWatchpoint.cpp
    lldb/trunk/source/Interpreter/CommandObjectRegexCommand.cpp
    lldb/trunk/source/Interpreter/CommandObjectScript.cpp
    lldb/trunk/source/Interpreter/CommandObjectScript.h
    lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
    lldb/trunk/source/Plugins/ScriptInterpreter/None/ScriptInterpreterNone.cpp
    lldb/trunk/source/Plugins/ScriptInterpreter/None/ScriptInterpreterNone.h
    lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
    lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h

Modified: lldb/trunk/include/lldb/Interpreter/CommandObject.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/CommandObject.h?rev=336955&r1=336954&r2=336955&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/CommandObject.h (original)
+++ lldb/trunk/include/lldb/Interpreter/CommandObject.h Thu Jul 12 15:28:52 2018
@@ -434,7 +434,8 @@ public:
   bool Execute(const char *args_string, CommandReturnObject &result) override;
 
 protected:
-  virtual bool DoExecute(const char *command, CommandReturnObject &result) = 0;
+  virtual bool DoExecute(llvm::StringRef command,
+                         CommandReturnObject &result) = 0;
 
   bool WantsRawCommandString() override { return true; }
 };

Modified: lldb/trunk/include/lldb/Interpreter/CommandObjectRegexCommand.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/CommandObjectRegexCommand.h?rev=336955&r1=336954&r2=336955&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/CommandObjectRegexCommand.h (original)
+++ lldb/trunk/include/lldb/Interpreter/CommandObjectRegexCommand.h Thu Jul 12 15:28:52 2018
@@ -43,7 +43,7 @@ public:
   int HandleCompletion(CompletionRequest &request) override;
 
 protected:
-  bool DoExecute(const char *command, CommandReturnObject &result) override;
+  bool DoExecute(llvm::StringRef command, CommandReturnObject &result) override;
 
   struct Entry {
     RegularExpression regex;

Modified: lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h?rev=336955&r1=336954&r2=336955&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h (original)
+++ lldb/trunk/include/lldb/Interpreter/ScriptInterpreter.h Thu Jul 12 15:28:52 2018
@@ -96,13 +96,13 @@ public:
   virtual bool Interrupt() { return false; }
 
   virtual bool ExecuteOneLine(
-      const char *command, CommandReturnObject *result,
+      llvm::StringRef command, CommandReturnObject *result,
       const ExecuteScriptOptions &options = ExecuteScriptOptions()) = 0;
 
   virtual void ExecuteInterpreterLoop() = 0;
 
   virtual bool ExecuteOneLineWithReturn(
-      const char *in_string, ScriptReturnType return_type, void *ret_value,
+      llvm::StringRef in_string, ScriptReturnType return_type, void *ret_value,
       const ExecuteScriptOptions &options = ExecuteScriptOptions()) {
     return true;
   }
@@ -343,7 +343,7 @@ public:
   }
 
   virtual bool
-  RunScriptBasedCommand(const char *impl_function, const char *args,
+  RunScriptBasedCommand(const char *impl_function, llvm::StringRef args,
                         ScriptedCommandSynchronicity synchronicity,
                         lldb_private::CommandReturnObject &cmd_retobj,
                         Status &error,
@@ -351,12 +351,11 @@ public:
     return false;
   }
 
-  virtual bool
-  RunScriptBasedCommand(StructuredData::GenericSP impl_obj_sp, const char *args,
-                        ScriptedCommandSynchronicity synchronicity,
-                        lldb_private::CommandReturnObject &cmd_retobj,
-                        Status &error,
-                        const lldb_private::ExecutionContext &exe_ctx) {
+  virtual bool RunScriptBasedCommand(
+      StructuredData::GenericSP impl_obj_sp, llvm::StringRef args,
+      ScriptedCommandSynchronicity synchronicity,
+      lldb_private::CommandReturnObject &cmd_retobj, Status &error,
+      const lldb_private::ExecutionContext &exe_ctx) {
     return false;
   }
 

Modified: lldb/trunk/source/Commands/CommandObjectCommands.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectCommands.cpp?rev=336955&r1=336954&r2=336955&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectCommands.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectCommands.cpp Thu Jul 12 15:28:52 2018
@@ -543,9 +543,9 @@ rather than using a positional placehold
   ~CommandObjectCommandsAlias() override = default;
 
 protected:
-  bool DoExecute(const char *raw_command_line,
+  bool DoExecute(llvm::StringRef raw_command_line,
                  CommandReturnObject &result) override {
-    if (!raw_command_line || !raw_command_line[0]) {
+    if (raw_command_line.empty()) {
       result.AppendError("'command alias' requires at least two arguments");
       return false;
     }
@@ -1275,7 +1275,7 @@ public:
   }
 
 protected:
-  bool DoExecute(const char *raw_command_line,
+  bool DoExecute(llvm::StringRef raw_command_line,
                  CommandReturnObject &result) override {
     ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
 
@@ -1364,7 +1364,7 @@ public:
   }
 
 protected:
-  bool DoExecute(const char *raw_command_line,
+  bool DoExecute(llvm::StringRef raw_command_line,
                  CommandReturnObject &result) override {
     ScriptInterpreter *scripter = m_interpreter.GetScriptInterpreter();
 

Modified: lldb/trunk/source/Commands/CommandObjectExpression.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectExpression.cpp?rev=336955&r1=336954&r2=336955&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectExpression.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectExpression.cpp Thu Jul 12 15:28:52 2018
@@ -318,7 +318,7 @@ CanBeUsedForElementCountPrinting(ValueOb
   return Status();
 }
 
-bool CommandObjectExpression::EvaluateExpression(const char *expr,
+bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr,
                                                  Stream *output_stream,
                                                  Stream *error_stream,
                                                  CommandReturnObject *result) {
@@ -508,19 +508,19 @@ void CommandObjectExpression::GetMultili
   debugger.PushIOHandler(io_handler_sp);
 }
 
-bool CommandObjectExpression::DoExecute(const char *command,
+bool CommandObjectExpression::DoExecute(llvm::StringRef command,
                                         CommandReturnObject &result) {
   m_fixed_expression.clear();
   auto exe_ctx = GetCommandInterpreter().GetExecutionContext();
   m_option_group.NotifyOptionParsingStarting(&exe_ctx);
 
-  if (command[0] == '\0') {
+  if (command.empty()) {
     GetMultilineExpression();
     return result.Succeeded();
   }
 
   OptionsWithRaw args(command);
-  const char *expr = args.GetRawPart().c_str();
+  llvm::StringRef expr = args.GetRawPart();
 
   if (args.HasArgs()) {
     if (!ParseOptionsAndNotify(args.GetArgs(), result, m_option_group, exe_ctx))
@@ -584,7 +584,7 @@ bool CommandObjectExpression::DoExecute(
       }
     }
     // No expression following options
-    else if (expr[0] == '\0') {
+    else if (expr.empty()) {
       GetMultilineExpression();
       return result.Succeeded();
     }

Modified: lldb/trunk/source/Commands/CommandObjectExpression.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectExpression.h?rev=336955&r1=336954&r2=336955&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectExpression.h (original)
+++ lldb/trunk/source/Commands/CommandObjectExpression.h Thu Jul 12 15:28:52 2018
@@ -72,9 +72,9 @@ protected:
   bool IOHandlerIsInputComplete(IOHandler &io_handler,
                                 StringList &lines) override;
 
-  bool DoExecute(const char *command, CommandReturnObject &result) override;
+  bool DoExecute(llvm::StringRef command, CommandReturnObject &result) override;
 
-  bool EvaluateExpression(const char *expr, Stream *output_stream,
+  bool EvaluateExpression(llvm::StringRef expr, Stream *output_stream,
                           Stream *error_stream,
                           CommandReturnObject *result = NULL);
 

Modified: lldb/trunk/source/Commands/CommandObjectPlatform.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectPlatform.cpp?rev=336955&r1=336954&r2=336955&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectPlatform.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectPlatform.cpp Thu Jul 12 15:28:52 2018
@@ -1742,14 +1742,14 @@ public:
 
   Options *GetOptions() override { return &m_options; }
 
-  bool DoExecute(const char *raw_command_line,
+  bool DoExecute(llvm::StringRef raw_command_line,
                  CommandReturnObject &result) override {
     ExecutionContext exe_ctx = GetCommandInterpreter().GetExecutionContext();
     m_options.NotifyOptionParsingStarting(&exe_ctx);
 
 
     // Print out an usage syntax on an empty command line.
-    if (raw_command_line[0] == '\0') {
+    if (raw_command_line.empty()) {
       result.GetOutputStream().Printf("%s\n", this->GetSyntax().str().c_str());
       return true;
     }

Modified: lldb/trunk/source/Commands/CommandObjectSettings.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectSettings.cpp?rev=336955&r1=336954&r2=336955&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectSettings.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectSettings.cpp Thu Jul 12 15:28:52 2018
@@ -188,7 +188,8 @@ insert-before or insert-after.");
   }
 
 protected:
-  bool DoExecute(const char *command, CommandReturnObject &result) override {
+  bool DoExecute(llvm::StringRef command,
+                 CommandReturnObject &result) override {
     Args cmd_args(command);
 
     // Process possible options.
@@ -467,7 +468,8 @@ public:
   }
 
 protected:
-  bool DoExecute(const char *command, CommandReturnObject &result) override {
+  bool DoExecute(llvm::StringRef command,
+                 CommandReturnObject &result) override {
     result.SetStatus(eReturnStatusSuccessFinishNoResult);
 
     Args cmd_args(command);
@@ -591,7 +593,8 @@ public:
   }
 
 protected:
-  bool DoExecute(const char *command, CommandReturnObject &result) override {
+  bool DoExecute(llvm::StringRef command,
+                 CommandReturnObject &result) override {
     result.SetStatus(eReturnStatusSuccessFinishNoResult);
 
     Args cmd_args(command);
@@ -698,7 +701,8 @@ public:
   }
 
 protected:
-  bool DoExecute(const char *command, CommandReturnObject &result) override {
+  bool DoExecute(llvm::StringRef command,
+                 CommandReturnObject &result) override {
     result.SetStatus(eReturnStatusSuccessFinishNoResult);
 
     Args cmd_args(command);
@@ -810,7 +814,8 @@ public:
   }
 
 protected:
-  bool DoExecute(const char *command, CommandReturnObject &result) override {
+  bool DoExecute(llvm::StringRef command,
+                 CommandReturnObject &result) override {
     result.SetStatus(eReturnStatusSuccessFinishNoResult);
 
     Args cmd_args(command);
@@ -911,7 +916,8 @@ public:
   }
 
 protected:
-  bool DoExecute(const char *command, CommandReturnObject &result) override {
+  bool DoExecute(llvm::StringRef command,
+                 CommandReturnObject &result) override {
     result.SetStatus(eReturnStatusSuccessFinishNoResult);
     Args cmd_args(command);
     const size_t argc = cmd_args.GetArgumentCount();

Modified: lldb/trunk/source/Commands/CommandObjectThread.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectThread.cpp?rev=336955&r1=336954&r2=336955&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectThread.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectThread.cpp Thu Jul 12 15:28:52 2018
@@ -1603,12 +1603,13 @@ public:
   Options *GetOptions() override { return &m_options; }
 
 protected:
-  bool DoExecute(const char *command, CommandReturnObject &result) override {
+  bool DoExecute(llvm::StringRef command,
+                 CommandReturnObject &result) override {
     // I am going to handle this by hand, because I don't want you to have to
     // say:
     // "thread return -- -5".
-    if (command[0] == '-' && command[1] == 'x') {
-      if (command && command[2] != '\0')
+    if (command.startswith("-x")) {
+      if (command.size() != 2U)
         result.AppendWarning("Return values ignored when returning from user "
                              "called expressions");
 
@@ -1645,7 +1646,7 @@ protected:
       return false;
     }
 
-    if (command && command[0] != '\0') {
+    if (!command.empty()) {
       Target *target = m_exe_ctx.GetTargetPtr();
       EvaluateExpressionOptions options;
 

Modified: lldb/trunk/source/Commands/CommandObjectType.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectType.cpp?rev=336955&r1=336954&r2=336955&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectType.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectType.cpp Thu Jul 12 15:28:52 2018
@@ -2862,9 +2862,9 @@ public:
     return m_cmd_help_long;
   }
 
-  bool DoExecute(const char *raw_command_line,
+  bool DoExecute(llvm::StringRef raw_command_line,
                  CommandReturnObject &result) override {
-    if (!raw_command_line || !raw_command_line[0]) {
+    if (raw_command_line.empty()) {
       result.SetError(
           "type lookup cannot be invoked without a type name as argument");
       return false;
@@ -2994,7 +2994,8 @@ public:
   ~CommandObjectFormatterInfo() override = default;
 
 protected:
-  bool DoExecute(const char *command, CommandReturnObject &result) override {
+  bool DoExecute(llvm::StringRef command,
+                 CommandReturnObject &result) override {
     TargetSP target_sp = m_interpreter.GetDebugger().GetSelectedTarget();
     Thread *thread = GetDefaultThread();
     if (!thread) {
@@ -3017,16 +3018,16 @@ protected:
           m_discovery_function(*result_valobj_sp);
       if (formatter_sp) {
         std::string description(formatter_sp->GetDescription());
-        result.AppendMessageWithFormat(
-            "%s applied to (%s) %s is: %s\n", m_formatter_name.c_str(),
-            result_valobj_sp->GetDisplayTypeName().AsCString("<unknown>"),
-            command, description.c_str());
+        result.GetOutputStream()
+            << m_formatter_name << " applied to ("
+            << result_valobj_sp->GetDisplayTypeName().AsCString("<unknown>")
+            << ") " << command << " is: " << description << "\n";
         result.SetStatus(lldb::eReturnStatusSuccessFinishResult);
       } else {
-        result.AppendMessageWithFormat(
-            "no %s applies to (%s) %s\n", m_formatter_name.c_str(),
-            result_valobj_sp->GetDisplayTypeName().AsCString("<unknown>"),
-            command);
+        result.GetOutputStream()
+            << "no " << m_formatter_name << " applies to ("
+            << result_valobj_sp->GetDisplayTypeName().AsCString("<unknown>")
+            << ") " << command << "\n";
         result.SetStatus(lldb::eReturnStatusSuccessFinishNoResult);
       }
       return true;

Modified: lldb/trunk/source/Commands/CommandObjectWatchpoint.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectWatchpoint.cpp?rev=336955&r1=336954&r2=336955&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectWatchpoint.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectWatchpoint.cpp Thu Jul 12 15:28:52 2018
@@ -1026,7 +1026,7 @@ Examples:
   Options *GetOptions() override { return &m_option_group; }
 
 protected:
-  bool DoExecute(const char *raw_command,
+  bool DoExecute(llvm::StringRef raw_command,
                  CommandReturnObject &result) override {
     auto exe_ctx = GetCommandInterpreter().GetExecutionContext();
     m_option_group.NotifyOptionParsingStarting(
@@ -1046,7 +1046,7 @@ protected:
 
     // If no argument is present, issue an error message.  There's no way to
     // set a watchpoint.
-    if (llvm::StringRef(raw_command).trim().empty()) {
+    if (raw_command.trim().empty()) {
       result.GetErrorStream().Printf("error: required argument missing; "
                                      "specify an expression to evaulate into "
                                      "the address to watch for\n");

Modified: lldb/trunk/source/Interpreter/CommandObjectRegexCommand.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/CommandObjectRegexCommand.cpp?rev=336955&r1=336954&r2=336955&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/CommandObjectRegexCommand.cpp (original)
+++ lldb/trunk/source/Interpreter/CommandObjectRegexCommand.cpp Thu Jul 12 15:28:52 2018
@@ -35,53 +35,47 @@ CommandObjectRegexCommand::CommandObject
 //----------------------------------------------------------------------
 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;
 }
 

Modified: lldb/trunk/source/Interpreter/CommandObjectScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/CommandObjectScript.cpp?rev=336955&r1=336954&r2=336955&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/CommandObjectScript.cpp (original)
+++ lldb/trunk/source/Interpreter/CommandObjectScript.cpp Thu Jul 12 15:28:52 2018
@@ -40,7 +40,7 @@ CommandObjectScript::CommandObjectScript
 
 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 @@ bool CommandObjectScript::DoExecute(cons
                                     // 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();

Modified: lldb/trunk/source/Interpreter/CommandObjectScript.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/CommandObjectScript.h?rev=336955&r1=336954&r2=336955&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/CommandObjectScript.h (original)
+++ lldb/trunk/source/Interpreter/CommandObjectScript.h Thu Jul 12 15:28:52 2018
@@ -30,7 +30,7 @@ public:
   ~CommandObjectScript() override;
 
 protected:
-  bool DoExecute(const char *command, CommandReturnObject &result) override;
+  bool DoExecute(llvm::StringRef command, CommandReturnObject &result) override;
 };
 
 } // namespace lldb_private

Modified: lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp?rev=336955&r1=336954&r2=336955&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp (original)
+++ lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp Thu Jul 12 15:28:52 2018
@@ -5244,8 +5244,9 @@ public:
 
   ~CommandObjectProcessGDBRemotePacketMonitor() {}
 
-  bool DoExecute(const char *command, CommandReturnObject &result) override {
-    if (command == NULL || command[0] == '\0') {
+  bool DoExecute(llvm::StringRef command,
+                 CommandReturnObject &result) override {
+    if (command.empty()) {
       result.AppendErrorWithFormat("'%s' takes a command string argument",
                                    m_cmd_name.c_str());
       result.SetStatus(eReturnStatusFailed);
@@ -5257,7 +5258,7 @@ public:
     if (process) {
       StreamString packet;
       packet.PutCString("qRcmd,");
-      packet.PutBytesAsRawHex8(command, strlen(command));
+      packet.PutBytesAsRawHex8(command.data(), command.size());
 
       bool send_async = true;
       StringExtractorGDBRemote response;

Modified: lldb/trunk/source/Plugins/ScriptInterpreter/None/ScriptInterpreterNone.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/None/ScriptInterpreterNone.cpp?rev=336955&r1=336954&r2=336955&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ScriptInterpreter/None/ScriptInterpreterNone.cpp (original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/None/ScriptInterpreterNone.cpp Thu Jul 12 15:28:52 2018
@@ -27,7 +27,7 @@ ScriptInterpreterNone::ScriptInterpreter
 
 ScriptInterpreterNone::~ScriptInterpreterNone() {}
 
-bool ScriptInterpreterNone::ExecuteOneLine(const char *command,
+bool ScriptInterpreterNone::ExecuteOneLine(llvm::StringRef command,
                                            CommandReturnObject *,
                                            const ExecuteScriptOptions &) {
   m_interpreter.GetDebugger().GetErrorFile()->PutCString(

Modified: lldb/trunk/source/Plugins/ScriptInterpreter/None/ScriptInterpreterNone.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/None/ScriptInterpreterNone.h?rev=336955&r1=336954&r2=336955&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ScriptInterpreter/None/ScriptInterpreterNone.h (original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/None/ScriptInterpreterNone.h Thu Jul 12 15:28:52 2018
@@ -25,7 +25,7 @@ public:
   ~ScriptInterpreterNone() override;
 
   bool ExecuteOneLine(
-      const char *command, CommandReturnObject *result,
+      llvm::StringRef command, CommandReturnObject *result,
       const ExecuteScriptOptions &options = ExecuteScriptOptions()) override;
 
   void ExecuteInterpreterLoop() override;

Modified: lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp?rev=336955&r1=336954&r2=336955&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp (original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp Thu Jul 12 15:28:52 2018
@@ -751,12 +751,12 @@ static void ReadThreadBytesReceived(void
 }
 
 bool ScriptInterpreterPython::ExecuteOneLine(
-    const char *command, CommandReturnObject *result,
+    llvm::StringRef command, CommandReturnObject *result,
     const ExecuteScriptOptions &options) {
   if (!m_valid_session)
     return false;
 
-  if (command && command[0]) {
+  if (!command.empty()) {
     // We want to call run_one_line, passing in the dictionary and the command
     // string.  We cannot do this through PyRun_SimpleString here because the
     // command string may contain escaped characters, and putting it inside
@@ -894,9 +894,11 @@ bool ScriptInterpreterPython::ExecuteOne
       return true;
 
     // The one-liner failed.  Append the error message.
-    if (result)
+    if (result) {
+      std::string command_str = command.str();
       result->AppendErrorWithFormat(
-          "python failed attempting to evaluate '%s'\n", command);
+          "python failed attempting to evaluate '%s'\n", command_str.c_str());
+    }
     return false;
   }
 
@@ -1021,7 +1023,7 @@ bool ScriptInterpreterPython::Interrupt(
   return false;
 }
 bool ScriptInterpreterPython::ExecuteOneLineWithReturn(
-    const char *in_string, ScriptInterpreter::ScriptReturnType return_type,
+    llvm::StringRef in_string, ScriptInterpreter::ScriptReturnType return_type,
     void *ret_value, const ExecuteScriptOptions &options) {
 
   Locker locker(this, ScriptInterpreterPython::Locker::AcquireLock |
@@ -1056,116 +1058,114 @@ bool ScriptInterpreterPython::ExecuteOne
   if (py_error.IsValid())
     PyErr_Clear();
 
-  if (in_string != nullptr) {
-    { // scope for PythonInputReaderManager
-      // PythonInputReaderManager py_input(options.GetEnableIO() ? this : NULL);
-      py_return.Reset(
-          PyRefType::Owned,
-          PyRun_String(in_string, Py_eval_input, globals.get(), locals.get()));
-      if (!py_return.IsValid()) {
-        py_error.Reset(PyRefType::Borrowed, PyErr_Occurred());
-        if (py_error.IsValid())
-          PyErr_Clear();
-
-        py_return.Reset(PyRefType::Owned,
-                        PyRun_String(in_string, Py_single_input, globals.get(),
-                                     locals.get()));
-      }
+  std::string as_string = in_string.str();
+  { // scope for PythonInputReaderManager
+    // PythonInputReaderManager py_input(options.GetEnableIO() ? this : NULL);
+    py_return.Reset(PyRefType::Owned,
+                    PyRun_String(as_string.c_str(), Py_eval_input,
+                                 globals.get(), locals.get()));
+    if (!py_return.IsValid()) {
+      py_error.Reset(PyRefType::Borrowed, PyErr_Occurred());
+      if (py_error.IsValid())
+        PyErr_Clear();
+
+      py_return.Reset(PyRefType::Owned,
+                      PyRun_String(as_string.c_str(), Py_single_input,
+                                   globals.get(), locals.get()));
     }
+  }
 
-    if (py_return.IsValid()) {
-      switch (return_type) {
-      case eScriptReturnTypeCharPtr: // "char *"
-      {
-        const char format[3] = "s#";
-        success = PyArg_Parse(py_return.get(), format, (char **)ret_value);
-        break;
-      }
-      case eScriptReturnTypeCharStrOrNone: // char* or NULL if py_return ==
-                                           // Py_None
-        {
-          const char format[3] = "z";
-          success = PyArg_Parse(py_return.get(), format, (char **)ret_value);
-          break;
-        }
-      case eScriptReturnTypeBool: {
-        const char format[2] = "b";
-        success = PyArg_Parse(py_return.get(), format, (bool *)ret_value);
-        break;
-      }
-      case eScriptReturnTypeShortInt: {
-        const char format[2] = "h";
-        success = PyArg_Parse(py_return.get(), format, (short *)ret_value);
-        break;
-      }
-      case eScriptReturnTypeShortIntUnsigned: {
-        const char format[2] = "H";
-        success =
-            PyArg_Parse(py_return.get(), format, (unsigned short *)ret_value);
-        break;
-      }
-      case eScriptReturnTypeInt: {
-        const char format[2] = "i";
-        success = PyArg_Parse(py_return.get(), format, (int *)ret_value);
-        break;
-      }
-      case eScriptReturnTypeIntUnsigned: {
-        const char format[2] = "I";
-        success =
-            PyArg_Parse(py_return.get(), format, (unsigned int *)ret_value);
-        break;
-      }
-      case eScriptReturnTypeLongInt: {
-        const char format[2] = "l";
-        success = PyArg_Parse(py_return.get(), format, (long *)ret_value);
-        break;
-      }
-      case eScriptReturnTypeLongIntUnsigned: {
-        const char format[2] = "k";
-        success =
-            PyArg_Parse(py_return.get(), format, (unsigned long *)ret_value);
-        break;
-      }
-      case eScriptReturnTypeLongLong: {
-        const char format[2] = "L";
-        success = PyArg_Parse(py_return.get(), format, (long long *)ret_value);
-        break;
-      }
-      case eScriptReturnTypeLongLongUnsigned: {
-        const char format[2] = "K";
-        success = PyArg_Parse(py_return.get(), format,
-                              (unsigned long long *)ret_value);
-        break;
-      }
-      case eScriptReturnTypeFloat: {
-        const char format[2] = "f";
-        success = PyArg_Parse(py_return.get(), format, (float *)ret_value);
-        break;
-      }
-      case eScriptReturnTypeDouble: {
-        const char format[2] = "d";
-        success = PyArg_Parse(py_return.get(), format, (double *)ret_value);
-        break;
-      }
-      case eScriptReturnTypeChar: {
-        const char format[2] = "c";
-        success = PyArg_Parse(py_return.get(), format, (char *)ret_value);
-        break;
-      }
-      case eScriptReturnTypeOpaqueObject: {
-        success = true;
-        PyObject *saved_value = py_return.get();
-        Py_XINCREF(saved_value);
-        *((PyObject **)ret_value) = saved_value;
-        break;
-      }
-      }
-
-      if (success)
-        ret_success = true;
-      else
-        ret_success = false;
+  if (py_return.IsValid()) {
+    switch (return_type) {
+    case eScriptReturnTypeCharPtr: // "char *"
+    {
+      const char format[3] = "s#";
+      success = PyArg_Parse(py_return.get(), format, (char **)ret_value);
+      break;
+    }
+    case eScriptReturnTypeCharStrOrNone: // char* or NULL if py_return ==
+                                         // Py_None
+    {
+      const char format[3] = "z";
+      success = PyArg_Parse(py_return.get(), format, (char **)ret_value);
+      break;
+    }
+    case eScriptReturnTypeBool: {
+      const char format[2] = "b";
+      success = PyArg_Parse(py_return.get(), format, (bool *)ret_value);
+      break;
+    }
+    case eScriptReturnTypeShortInt: {
+      const char format[2] = "h";
+      success = PyArg_Parse(py_return.get(), format, (short *)ret_value);
+      break;
+    }
+    case eScriptReturnTypeShortIntUnsigned: {
+      const char format[2] = "H";
+      success =
+          PyArg_Parse(py_return.get(), format, (unsigned short *)ret_value);
+      break;
+    }
+    case eScriptReturnTypeInt: {
+      const char format[2] = "i";
+      success = PyArg_Parse(py_return.get(), format, (int *)ret_value);
+      break;
+    }
+    case eScriptReturnTypeIntUnsigned: {
+      const char format[2] = "I";
+      success = PyArg_Parse(py_return.get(), format, (unsigned int *)ret_value);
+      break;
+    }
+    case eScriptReturnTypeLongInt: {
+      const char format[2] = "l";
+      success = PyArg_Parse(py_return.get(), format, (long *)ret_value);
+      break;
     }
+    case eScriptReturnTypeLongIntUnsigned: {
+      const char format[2] = "k";
+      success =
+          PyArg_Parse(py_return.get(), format, (unsigned long *)ret_value);
+      break;
+    }
+    case eScriptReturnTypeLongLong: {
+      const char format[2] = "L";
+      success = PyArg_Parse(py_return.get(), format, (long long *)ret_value);
+      break;
+    }
+    case eScriptReturnTypeLongLongUnsigned: {
+      const char format[2] = "K";
+      success =
+          PyArg_Parse(py_return.get(), format, (unsigned long long *)ret_value);
+      break;
+    }
+    case eScriptReturnTypeFloat: {
+      const char format[2] = "f";
+      success = PyArg_Parse(py_return.get(), format, (float *)ret_value);
+      break;
+    }
+    case eScriptReturnTypeDouble: {
+      const char format[2] = "d";
+      success = PyArg_Parse(py_return.get(), format, (double *)ret_value);
+      break;
+    }
+    case eScriptReturnTypeChar: {
+      const char format[2] = "c";
+      success = PyArg_Parse(py_return.get(), format, (char *)ret_value);
+      break;
+    }
+    case eScriptReturnTypeOpaqueObject: {
+      success = true;
+      PyObject *saved_value = py_return.get();
+      Py_XINCREF(saved_value);
+      *((PyObject **)ret_value) = saved_value;
+      break;
+    }
+    }
+
+    if (success)
+      ret_success = true;
+    else
+      ret_success = false;
   }
 
   py_error.Reset(PyRefType::Borrowed, PyErr_Occurred());
@@ -2779,7 +2779,7 @@ ScriptInterpreterPython::SynchronicityHa
 }
 
 bool ScriptInterpreterPython::RunScriptBasedCommand(
-    const char *impl_function, const char *args,
+    const char *impl_function, llvm::StringRef args,
     ScriptedCommandSynchronicity synchronicity,
     lldb_private::CommandReturnObject &cmd_retobj, Status &error,
     const lldb_private::ExecutionContext &exe_ctx) {
@@ -2813,9 +2813,10 @@ bool ScriptInterpreterPython::RunScriptB
 
     SynchronicityHandler synch_handler(debugger_sp, synchronicity);
 
-    ret_val =
-        g_swig_call_command(impl_function, m_dictionary_name.c_str(),
-                            debugger_sp, args, cmd_retobj, exe_ctx_ref_sp);
+    std::string args_str = args.str();
+    ret_val = g_swig_call_command(impl_function, m_dictionary_name.c_str(),
+                                  debugger_sp, args_str.c_str(), cmd_retobj,
+                                  exe_ctx_ref_sp);
   }
 
   if (!ret_val)
@@ -2827,7 +2828,7 @@ bool ScriptInterpreterPython::RunScriptB
 }
 
 bool ScriptInterpreterPython::RunScriptBasedCommand(
-    StructuredData::GenericSP impl_obj_sp, const char *args,
+    StructuredData::GenericSP impl_obj_sp, llvm::StringRef args,
     ScriptedCommandSynchronicity synchronicity,
     lldb_private::CommandReturnObject &cmd_retobj, Status &error,
     const lldb_private::ExecutionContext &exe_ctx) {
@@ -2861,8 +2862,10 @@ bool ScriptInterpreterPython::RunScriptB
 
     SynchronicityHandler synch_handler(debugger_sp, synchronicity);
 
+    std::string args_str = args.str();
     ret_val = g_swig_call_command_object(impl_obj_sp->GetValue(), debugger_sp,
-                                         args, cmd_retobj, exe_ctx_ref_sp);
+                                         args_str.c_str(), cmd_retobj,
+                                         exe_ctx_ref_sp);
   }
 
   if (!ret_val)

Modified: lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h?rev=336955&r1=336954&r2=336955&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h (original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h Thu Jul 12 15:28:52 2018
@@ -151,14 +151,14 @@ public:
   bool Interrupt() override;
 
   bool ExecuteOneLine(
-      const char *command, CommandReturnObject *result,
+      llvm::StringRef command, CommandReturnObject *result,
       const ExecuteScriptOptions &options = ExecuteScriptOptions()) override;
 
   void ExecuteInterpreterLoop() override;
 
   bool ExecuteOneLineWithReturn(
-      const char *in_string, ScriptInterpreter::ScriptReturnType return_type,
-      void *ret_value,
+      llvm::StringRef in_string,
+      ScriptInterpreter::ScriptReturnType return_type, void *ret_value,
       const ExecuteScriptOptions &options = ExecuteScriptOptions()) override;
 
   lldb_private::Status ExecuteMultipleLines(
@@ -259,18 +259,17 @@ public:
   GetSyntheticTypeName(const StructuredData::ObjectSP &implementor) override;
 
   bool
-  RunScriptBasedCommand(const char *impl_function, const char *args,
+  RunScriptBasedCommand(const char *impl_function, llvm::StringRef args,
                         ScriptedCommandSynchronicity synchronicity,
                         lldb_private::CommandReturnObject &cmd_retobj,
                         Status &error,
                         const lldb_private::ExecutionContext &exe_ctx) override;
 
-  bool
-  RunScriptBasedCommand(StructuredData::GenericSP impl_obj_sp, const char *args,
-                        ScriptedCommandSynchronicity synchronicity,
-                        lldb_private::CommandReturnObject &cmd_retobj,
-                        Status &error,
-                        const lldb_private::ExecutionContext &exe_ctx) override;
+  bool RunScriptBasedCommand(
+      StructuredData::GenericSP impl_obj_sp, llvm::StringRef args,
+      ScriptedCommandSynchronicity synchronicity,
+      lldb_private::CommandReturnObject &cmd_retobj, Status &error,
+      const lldb_private::ExecutionContext &exe_ctx) override;
 
   Status GenerateFunction(const char *signature,
                           const StringList &input) override;




More information about the lldb-commits mailing list