[Lldb-commits] [PATCH] D48295: [WIP] Implement new ReturnMIStatus method of CMICmdBase class.

Alexander Polyakov via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Tue Jun 19 06:18:00 PDT 2018


apolyakov updated this revision to Diff 151906.
apolyakov added a comment.

Attached another realization of `CMICmdBase::ReturnMIStatus`. It uses lambda functions to get user defined actions.

It works, but, as you may see, I had to define `auto error_handler = ...` in `Execute` function since in C++ we can't do something like `return ReturnMIStatus(success_handler,, error)`.

Base on this, I see a few options:

1. keep it in current state;
2. use llvm::Optional instead of default arguments. We still will be needed to pass all three parameters, but in this case user could do following: `return ReturnMIStatus(success_handler, llvm::Optional::None, error)`.
3. go back to simple implementation of `ReturnMIStatus`:

  bool CMICmdBase::ReturnMIStatus(const SBError &error) {
    if (error.Success())
      return MIstatus::success;
  
    SetError(error.GetCString());
    return MIstatus::failure;
  }

4. discard this idea and keep duplicating code;


https://reviews.llvm.org/D48295

Files:
  tools/lldb-mi/MICmdBase.cpp
  tools/lldb-mi/MICmdBase.h
  tools/lldb-mi/MICmdCmdExec.cpp


Index: tools/lldb-mi/MICmdCmdExec.cpp
===================================================================
--- tools/lldb-mi/MICmdCmdExec.cpp
+++ tools/lldb-mi/MICmdCmdExec.cpp
@@ -235,21 +235,22 @@
 bool CMICmdCmdExecContinue::Execute() {
   lldb::SBError error =
       CMICmnLLDBDebugSessionInfo::Instance().GetProcess().Continue();
- 
-  if (error.Success()) {
-    // CODETAG_DEBUG_SESSION_RUNNING_PROG_RECEIVED_SIGINT_PAUSE_PROGRAM
+
+  auto success_handler = [this] {
     if (!CMIDriver::Instance().SetDriverStateRunningDebugging()) {
       const CMIUtilString &rErrMsg(CMIDriver::Instance().GetErrorDescription());
-      SetError(CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_SET_NEW_DRIVER_STATE),
-                                     m_cmdData.strMiCmd.c_str(),
-                                     rErrMsg.c_str()));
+      this->SetError(CMIUtilString::Format(
+          MIRSRC(IDS_CMD_ERR_SET_NEW_DRIVER_STATE),
+          this->m_cmdData.strMiCmd.c_str(),
+          rErrMsg.c_str()));
       return MIstatus::failure;
     }
     return MIstatus::success;
-  }
+  };
 
-  SetError(error.GetCString());
-  return MIstatus::failure;
+  auto error_handler = [] { return MIstatus::failure; };
+
+  return ReturnMIStatus(success_handler, error_handler, error); 
 }
 
 //++
Index: tools/lldb-mi/MICmdBase.h
===================================================================
--- tools/lldb-mi/MICmdBase.h
+++ tools/lldb-mi/MICmdBase.h
@@ -12,6 +12,8 @@
 // C Includes
 // C++ Includes
 // Other libraries and framework includes
+#include "lldb/API/SBError.h"
+
 // Project includes
 #include "MICmdArgSet.h"
 #include "MICmdData.h"
@@ -80,6 +82,11 @@
   // Methods:
 protected:
   void SetError(const CMIUtilString &rErrMsg);
+  bool ReturnMIStatus(const std::function<bool()> &success_handler =
+                      [] { return MIstatus::success; },
+                      const std::function<bool()> &error_handler =
+                      [] { return MIstatus::failure; },
+                      const lldb::SBError error = lldb::SBError());
   template <class T> T *GetOption(const CMIUtilString &vStrOptionName);
   bool ParseValidateCmdOptions();
 
Index: tools/lldb-mi/MICmdBase.cpp
===================================================================
--- tools/lldb-mi/MICmdBase.cpp
+++ tools/lldb-mi/MICmdBase.cpp
@@ -214,6 +214,25 @@
 
 //++
 //------------------------------------------------------------------------------------
+// Details: Short cut function to check MI command's execute status and
+//          set an error in case of failure.
+// Type:    Method.
+// Args:    error - (R) Error description object.
+// Return:  bool.
+// Throws:  None.
+//--
+bool CMICmdBase::ReturnMIStatus(const std::function<bool()> &success_handler,
+                                const std::function<bool()> &error_handler,
+                                const lldb::SBError error) {
+  if (error.Success())
+    return success_handler();
+
+  SetError(error.GetCString());
+  return error_handler();
+}
+
+//++
+//------------------------------------------------------------------------------------
 // Details: Ask a command to provide its unique identifier.
 // Type:    Method.
 // Args:    A unique identifier for this command class.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D48295.151906.patch
Type: text/x-patch
Size: 3258 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20180619/ecdaad06/attachment-0001.bin>


More information about the lldb-commits mailing list