ki.stfu created this revision.
ki.stfu added reviewers: abidh, brucem.
ki.stfu added subscribers: abidh, brucem, lldb-commits.

Allow to construct CMIUtilString using std::string directly (MI)

This patch cleans up lldb-mi code, and serves to simplify
the following case:
```
  std::string strGoodbye = "!Hello";
  CMIUtilString strHello = strGoodbye.substr(1).c_str();
```

With CMIUtilString(std::string) we can omit .c_str():
```
  std::string strGoodbye = "!Hello";
  CMIUtilString strHello = strGoodbye.substr(1);
```

http://reviews.llvm.org/D13158

Files:
  tools/lldb-mi/MICmdArgContext.cpp
  tools/lldb-mi/MICmdArgValOptionLong.cpp
  tools/lldb-mi/MICmdArgValOptionShort.cpp
  tools/lldb-mi/MICmdArgValThreadGrp.cpp
  tools/lldb-mi/MICmdCmdData.cpp
  tools/lldb-mi/MICmdCmdVar.cpp
  tools/lldb-mi/MICmdInterpreter.cpp
  tools/lldb-mi/MICmnLLDBDebugger.cpp
  tools/lldb-mi/MICmnLLDBDebuggerHandleEvents.cpp
  tools/lldb-mi/MICmnLLDBUtilSBValue.cpp
  tools/lldb-mi/MIDriver.cpp
  tools/lldb-mi/MIUtilString.cpp
  tools/lldb-mi/MIUtilString.h

Index: tools/lldb-mi/MIUtilString.h
===================================================================
--- tools/lldb-mi/MIUtilString.h
+++ tools/lldb-mi/MIUtilString.h
@@ -45,6 +45,7 @@
     /* ctor */ CMIUtilString(const char *vpData);
     /* ctor */ CMIUtilString(const char *const *vpData);
     /* ctor */ CMIUtilString(const char *vpData, size_t nLen);
+    /* ctor */ CMIUtilString(const std::string& vrStr);
     //
     bool ExtractNumber(MIint64 &vwrNumber) const;
     CMIUtilString FindAndReplace(const CMIUtilString &vFind, const CMIUtilString &vReplaceWith) const;
Index: tools/lldb-mi/MIUtilString.cpp
===================================================================
--- tools/lldb-mi/MIUtilString.cpp
+++ tools/lldb-mi/MIUtilString.cpp
@@ -68,6 +68,18 @@
 }
 
 //++ ------------------------------------------------------------------------------------
+// Details: CMIUtilString constructor.
+// Type:    Method.
+// Args:    vpStr  - Text data.
+// Return:  None.
+// Throws:  None.
+//--
+CMIUtilString::CMIUtilString(const std::string& vrStr)
+    : std::string(vrStr)
+{
+}
+
+//++ ------------------------------------------------------------------------------------
 // Details: CMIUtilString assignment operator.
 // Type:    Method.
 // Args:    vpRhs   - Pointer to UTF8 text data.
@@ -243,7 +255,7 @@
         // Extract string between delimiters
         const size_t nSectionLen(nNextDelimiterPos - nSectionPos);
         const std::string strSection(substr(nSectionPos, nSectionLen));
-        vwVecSplits.push_back(strSection.c_str());
+        vwVecSplits.push_back(strSection);
 
         // Next
         nOffset = nNextDelimiterPos + 1;
@@ -299,7 +311,7 @@
         // Extract string between delimiters
         const size_t nSectionLen(nNextDelimiterPos - nSectionPos);
         const std::string strSection(substr(nSectionPos, nSectionLen));
-        vwVecSplits.push_back(strSection.c_str());
+        vwVecSplits.push_back(strSection);
 
         // Next
         nOffset = nNextDelimiterPos + 1;
@@ -337,7 +349,7 @@
     if (nPos == std::string::npos)
         return *this;
 
-    const CMIUtilString strNew(substr(0, nPos).c_str());
+    const CMIUtilString strNew(substr(0, nPos));
 
     return strNew;
 }
@@ -542,12 +554,12 @@
     const size_t nPos = find_last_not_of(pWhiteSpace);
     if (nPos != std::string::npos)
     {
-        strNew = substr(0, nPos + 1).c_str();
+        strNew = substr(0, nPos + 1);
     }
     const size_t nPos2 = strNew.find_first_not_of(pWhiteSpace);
     if (nPos2 != std::string::npos)
     {
-        strNew = strNew.substr(nPos2).c_str();
+        strNew = strNew.substr(nPos2);
     }
 
     return strNew;
@@ -568,7 +580,7 @@
     if (nLen > 1)
     {
         if ((strNew[0] == vChar) && (strNew[nLen - 1] == vChar))
-            strNew = strNew.substr(1, nLen - 2).c_str();
+            strNew = strNew.substr(1, nLen - 2);
     }
 
     return strNew;
Index: tools/lldb-mi/MIDriver.cpp
===================================================================
--- tools/lldb-mi/MIDriver.cpp
+++ tools/lldb-mi/MIDriver.cpp
@@ -893,7 +893,7 @@
    const std::string vToken(vTextLine.begin(), vTextLine.begin() + nCommandOffset);
    // 001target create "/path/to/file"
    //    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- CLI command
-   const CMIUtilString vCliCommand(std::string(vTextLine, nCommandOffset).c_str());
+   const CMIUtilString vCliCommand(std::string(vTextLine, nCommandOffset));
 
    // 5. Escape special characters and embed the command in a string
    // Result: it looks like -- target create \"/path/to/file\".
Index: tools/lldb-mi/MICmnLLDBUtilSBValue.cpp
===================================================================
--- tools/lldb-mi/MICmnLLDBUtilSBValue.cpp
+++ tools/lldb-mi/MICmnLLDBUtilSBValue.cpp
@@ -505,7 +505,7 @@
         addr += sizeof(ch);
     }
 
-    return result.c_str();
+    return result;
 }
 
 //++ ------------------------------------------------------------------------------------
Index: tools/lldb-mi/MICmnLLDBDebuggerHandleEvents.cpp
===================================================================
--- tools/lldb-mi/MICmnLLDBDebuggerHandleEvents.cpp
+++ tools/lldb-mi/MICmnLLDBDebuggerHandleEvents.cpp
@@ -1572,7 +1572,7 @@
             if (nNewLine == std::string::npos)
                 break;
 
-            const CMIUtilString line(text.substr(0, nNewLine + 1).c_str());
+            const CMIUtilString line(text.substr(0, nNewLine + 1));
             text.erase(0, nNewLine + 1);
             const bool bEscapeQuotes(true);
             CMICmnMIValueConst miValueConst(line.Escape(bEscapeQuotes));
@@ -1625,7 +1625,7 @@
             if (nNewLine == std::string::npos)
                 break;
 
-            const CMIUtilString line(text.substr(0, nNewLine + 1).c_str());
+            const CMIUtilString line(text.substr(0, nNewLine + 1));
             const bool bEscapeQuotes(true);
             CMICmnMIValueConst miValueConst(line.Escape(bEscapeQuotes));
             CMICmnMIOutOfBandRecord miOutOfBandRecord(CMICmnMIOutOfBandRecord::eOutOfBand_TargetStreamOutput, miValueConst);
Index: tools/lldb-mi/MICmnLLDBDebugger.cpp
===================================================================
--- tools/lldb-mi/MICmnLLDBDebugger.cpp
+++ tools/lldb-mi/MICmnLLDBDebugger.cpp
@@ -585,7 +585,7 @@
     while (it != m_mapIdToEventMask.end())
     {
         const CMIUtilString &rId((*it).first);
-        if (rId.find(vBroadcasterClass.c_str()) != std::string::npos)
+        if (rId.find(vBroadcasterClass) != std::string::npos)
         {
             const MIuint clientsMask = (*it).second;
             mask |= clientsMask;
@@ -678,9 +678,7 @@
         return MIstatus::failure;
     }
 
-    CMIUtilString strId(vBroadcasterClass.c_str());
-    strId += vClientName;
-
+    const CMIUtilString strId(vBroadcasterClass + vClientName);
     const MapIdToEventMask_t::const_iterator it = m_mapIdToEventMask.find(strId);
     if (it != m_mapIdToEventMask.end())
     {
Index: tools/lldb-mi/MICmdInterpreter.cpp
===================================================================
--- tools/lldb-mi/MICmdInterpreter.cpp
+++ tools/lldb-mi/MICmdInterpreter.cpp
@@ -160,7 +160,7 @@
     if (MiHasCmdTokenPresent(vTextLine))
     {
         const std::string strNum = vTextLine.substr(0, nPos);
-        if (!CMIUtilString(strNum.c_str()).IsNumber())
+        if (!CMIUtilString(strNum).IsNumber())
             return false;
 
         m_miCmdData.strMiCmdToken = strNum.c_str();
@@ -256,20 +256,20 @@
     {
         if (nPos2 == nLen)
             return false;
-        const CMIUtilString cmd = CMIUtilString(vTextLine.substr(nPos + 1, nPos2 - nPos - 1).c_str());
+        const CMIUtilString cmd = CMIUtilString(vTextLine.substr(nPos + 1, nPos2 - nPos - 1));
         if (cmd.empty())
             return false;
 
         m_miCmdData.strMiCmd = cmd;
 
         if (nPos2 < nLen)
-            m_miCmdData.strMiCmdOption = CMIUtilString(vTextLine.substr(nPos2 + 1, nLen - nPos2 - 1).c_str());
+            m_miCmdData.strMiCmdOption = CMIUtilString(vTextLine.substr(nPos2 + 1, nLen - nPos2 - 1));
 
         bFoundCmd = true;
     }
     else
     {
-        const CMIUtilString cmd = CMIUtilString(vTextLine.substr(nPos + 1, nLen - nPos - 1).c_str());
+        const CMIUtilString cmd = CMIUtilString(vTextLine.substr(nPos + 1, nLen - nPos - 1));
         if (cmd.empty())
             return false;
         m_miCmdData.strMiCmd = cmd;
Index: tools/lldb-mi/MICmdCmdVar.cpp
===================================================================
--- tools/lldb-mi/MICmdCmdVar.cpp
+++ tools/lldb-mi/MICmdCmdVar.cpp
@@ -179,7 +179,7 @@
 
     if (rStrExpression[0] == '$')
     {
-        const CMIUtilString rStrRegister(rStrExpression.substr(1).c_str());
+        const CMIUtilString rStrRegister(rStrExpression.substr(1));
         value = frame.FindRegister(rStrRegister.c_str());
     }
     else
Index: tools/lldb-mi/MICmdCmdData.cpp
===================================================================
--- tools/lldb-mi/MICmdCmdData.cpp
+++ tools/lldb-mi/MICmdCmdData.cpp
@@ -1613,7 +1613,7 @@
         // Parse argument:
         // *0x12345
         //  ^^^^^^^ -- address
-        const CMIUtilString strAddress(strLocation.c_str() + 1);
+        const CMIUtilString strAddress(strLocation.substr(1));
         strCmdOptionsLocation = CMIUtilString::Format("--address %s", strAddress.c_str());
     }
     else
@@ -1629,8 +1629,8 @@
         // hello.cpp:5
         // ^^^^^^^^^ -- file
         //           ^ -- line
-        const CMIUtilString strFile(strLocation.substr(0, nLineStartPos).c_str());
-        const CMIUtilString strLine(strLocation.substr(nLineStartPos + 1).c_str());
+        const CMIUtilString strFile(strLocation.substr(0, nLineStartPos));
+        const CMIUtilString strLine(strLocation.substr(nLineStartPos + 1));
         strCmdOptionsLocation = CMIUtilString::Format("--file \"%s\" --line %s", strFile.AddSlashes().c_str(), strLine.c_str());
     }
     const CMIUtilString strCmd(CMIUtilString::Format("target modules lookup -v %s", strCmdOptionsLocation.c_str()));
Index: tools/lldb-mi/MICmdArgValThreadGrp.cpp
===================================================================
--- tools/lldb-mi/MICmdArgValThreadGrp.cpp
+++ tools/lldb-mi/MICmdArgValThreadGrp.cpp
@@ -121,7 +121,7 @@
     if (nPos != 0)
         return false;
 
-    const CMIUtilString strNum = vrTxt.substr(1).c_str();
+    const CMIUtilString strNum = vrTxt.substr(1);
     if (!strNum.IsNumber())
         return false;
 
@@ -139,7 +139,7 @@
 bool
 CMICmdArgValThreadGrp::ExtractNumber(const CMIUtilString &vrTxt)
 {
-    const CMIUtilString strNum = vrTxt.substr(1).c_str();
+    const CMIUtilString strNum = vrTxt.substr(1);
     MIint64 nNumber = 0;
     bool bOk = strNum.ExtractNumber(nNumber);
     if (bOk)
Index: tools/lldb-mi/MICmdArgValOptionShort.cpp
===================================================================
--- tools/lldb-mi/MICmdArgValOptionShort.cpp
+++ tools/lldb-mi/MICmdArgValOptionShort.cpp
@@ -115,6 +115,6 @@
 bool
 CMICmdArgValOptionShort::ArgNameMatch(const CMIUtilString &vrTxt) const
 {
-    const CMIUtilString strArg = vrTxt.substr(1).c_str();
+    const CMIUtilString strArg = vrTxt.substr(1);
     return (strArg == GetName());
 }
Index: tools/lldb-mi/MICmdArgValOptionLong.cpp
===================================================================
--- tools/lldb-mi/MICmdArgValOptionLong.cpp
+++ tools/lldb-mi/MICmdArgValOptionLong.cpp
@@ -261,7 +261,7 @@
     if (vrTxt.length() < 3)
         return false;
 
-    const CMIUtilString strArg = vrTxt.substr(2).c_str();
+    const CMIUtilString strArg = vrTxt.substr(2);
     if (strArg.IsNumber())
         return false;
 
@@ -293,7 +293,7 @@
 bool
 CMICmdArgValOptionLong::ArgNameMatch(const CMIUtilString &vrTxt) const
 {
-    const CMIUtilString strArg = vrTxt.substr(2).c_str();
+    const CMIUtilString strArg = vrTxt.substr(2);
     return (strArg == GetName());
 }
 
Index: tools/lldb-mi/MICmdArgContext.cpp
===================================================================
--- tools/lldb-mi/MICmdArgContext.cpp
+++ tools/lldb-mi/MICmdArgContext.cpp
@@ -130,7 +130,7 @@
     }
 
     const size_t nPosEnd = nLen + nExtraSpace;
-    m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.replace(nPos, nPosEnd, "").c_str();
+    m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.replace(nPos, nPosEnd, "");
     m_strCmdArgsAndOptions = m_strCmdArgsAndOptions.Trim();
 
     return MIstatus::success;
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to