Skip to content

Commit

Permalink
Allow to construct CMIUtilString using std::string directly + cleanup…
Browse files Browse the repository at this point in the history
… CMIUtilString (MI)

Summary:
Allow to construct CMIUtilString using std::string directly + cleanup CMIUtilString (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));
```

Also, it removes 2 ctors because they aren't needed:
# CMIUtilString::CMIUtilString(const char *const *vpData)
# CMIUtilString::CMIUtilString(const char *vpData, size_t nLen)
and cleans up CMIUtilString::operator=(const std::string &vrRhs).

Reviewers: brucem, abidh

Subscribers: lldb-commits, brucem, abidh

Differential Revision: http://reviews.llvm.org/D13158

llvm-svn: 248566
  • Loading branch information
k15tfu committed Sep 25, 2015
1 parent f69d45e commit b2b0170
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 51 deletions.
2 changes: 1 addition & 1 deletion lldb/tools/lldb-mi/MICmdArgContext.cpp
Expand Up @@ -130,7 +130,7 @@ CMICmdArgContext::RemoveArg(const CMIUtilString &vArg)
}

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;
Expand Down
4 changes: 2 additions & 2 deletions lldb/tools/lldb-mi/MICmdArgValOptionLong.cpp
Expand Up @@ -261,7 +261,7 @@ CMICmdArgValOptionLong::IsArgLongOption(const CMIUtilString &vrTxt) const
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;

Expand Down Expand Up @@ -293,7 +293,7 @@ CMICmdArgValOptionLong::IsArgOptionCorrect(const CMIUtilString &vrTxt) const
bool
CMICmdArgValOptionLong::ArgNameMatch(const CMIUtilString &vrTxt) const
{
const CMIUtilString strArg = vrTxt.substr(2).c_str();
const CMIUtilString strArg = vrTxt.substr(2);
return (strArg == GetName());
}

Expand Down
2 changes: 1 addition & 1 deletion lldb/tools/lldb-mi/MICmdArgValOptionShort.cpp
Expand Up @@ -115,6 +115,6 @@ CMICmdArgValOptionShort::IsArgOptionCorrect(const CMIUtilString &vrTxt) const
bool
CMICmdArgValOptionShort::ArgNameMatch(const CMIUtilString &vrTxt) const
{
const CMIUtilString strArg = vrTxt.substr(1).c_str();
const CMIUtilString strArg = vrTxt.substr(1);
return (strArg == GetName());
}
4 changes: 2 additions & 2 deletions lldb/tools/lldb-mi/MICmdArgValThreadGrp.cpp
Expand Up @@ -121,7 +121,7 @@ CMICmdArgValThreadGrp::IsArgThreadGrp(const CMIUtilString &vrTxt) const
if (nPos != 0)
return false;

const CMIUtilString strNum = vrTxt.substr(1).c_str();
const CMIUtilString strNum = vrTxt.substr(1);
if (!strNum.IsNumber())
return false;

Expand All @@ -139,7 +139,7 @@ CMICmdArgValThreadGrp::IsArgThreadGrp(const CMIUtilString &vrTxt) const
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)
Expand Down
6 changes: 3 additions & 3 deletions lldb/tools/lldb-mi/MICmdCmdData.cpp
Expand Up @@ -1613,7 +1613,7 @@ CMICmdCmdDataInfoLine::Execute()
// 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
Expand All @@ -1629,8 +1629,8 @@ CMICmdCmdDataInfoLine::Execute()
// 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()));
Expand Down
2 changes: 1 addition & 1 deletion lldb/tools/lldb-mi/MICmdCmdVar.cpp
Expand Up @@ -179,7 +179,7 @@ CMICmdCmdVarCreate::Execute()

if (rStrExpression[0] == '$')
{
const CMIUtilString rStrRegister(rStrExpression.substr(1).c_str());
const CMIUtilString rStrRegister(rStrExpression.substr(1));
value = frame.FindRegister(rStrRegister.c_str());
}
else
Expand Down
10 changes: 5 additions & 5 deletions lldb/tools/lldb-mi/MICmdInterpreter.cpp
Expand Up @@ -160,10 +160,10 @@ CMICmdInterpreter::MiHasCmdTokenEndingHyphen(const CMIUtilString &vTextLine)
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();
m_miCmdData.strMiCmdToken = strNum;
}

m_miCmdData.bMIOldStyle = false;
Expand Down Expand Up @@ -256,20 +256,20 @@ CMICmdInterpreter::MiHasCmd(const CMIUtilString &vTextLine)
{
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;
Expand Down
6 changes: 2 additions & 4 deletions lldb/tools/lldb-mi/MICmnLLDBDebugger.cpp
Expand Up @@ -585,7 +585,7 @@ CMICmnLLDBDebugger::ClientGetMaskForAllClients(const CMIUtilString &vBroadcaster
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;
Expand Down Expand Up @@ -678,9 +678,7 @@ CMICmnLLDBDebugger::ClientGetTheirMask(const CMIUtilString &vClientName, const C
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())
{
Expand Down
4 changes: 2 additions & 2 deletions lldb/tools/lldb-mi/MICmnLLDBDebuggerHandleEvents.cpp
Expand Up @@ -1572,7 +1572,7 @@ CMICmnLLDBDebuggerHandleEvents::GetProcessStdout()
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));
Expand Down Expand Up @@ -1625,7 +1625,7 @@ CMICmnLLDBDebuggerHandleEvents::GetProcessStderr()
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);
Expand Down
2 changes: 1 addition & 1 deletion lldb/tools/lldb-mi/MICmnLLDBUtilSBValue.cpp
Expand Up @@ -505,7 +505,7 @@ CMICmnLLDBUtilSBValue::ReadCStringFromHostMemory(lldb::SBValue &vrValue, const M
addr += sizeof(ch);
}

return result.c_str();
return result;
}

//++ ------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion lldb/tools/lldb-mi/MIDriver.cpp
Expand Up @@ -893,7 +893,7 @@ CMIDriver::WrapCLICommandIntoMICommand(const CMIUtilString &vTextLine) const
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\".
Expand Down
35 changes: 9 additions & 26 deletions lldb/tools/lldb-mi/MIUtilString.cpp
Expand Up @@ -45,25 +45,12 @@ CMIUtilString::CMIUtilString(const char *vpData)
//++ ------------------------------------------------------------------------------------
// Details: CMIUtilString constructor.
// Type: Method.
// Args: vpData - Pointer to UTF8 text data.
// Return: None.
// Throws: None.
//--
CMIUtilString::CMIUtilString(const char *const *vpData)
: std::string((const char *)vpData)
{
}

//++ ------------------------------------------------------------------------------------
// Details: CMIUtilString constructor.
// Type: Method.
// Args: vpData - Pointer to UTF8 text data.
// nLen - Length of string.
// Args: vpStr - Text data.
// Return: None.
// Throws: None.
//--
CMIUtilString::CMIUtilString(const char *vpData, size_t nLen)
: std::string(vpData, nLen)
CMIUtilString::CMIUtilString(const std::string& vrStr)
: std::string(vrStr)
{
}

Expand Down Expand Up @@ -96,11 +83,7 @@ CMIUtilString &CMIUtilString::operator=(const char *vpRhs)
//--
CMIUtilString &CMIUtilString::operator=(const std::string &vrRhs)
{
if (*this == vrRhs)
return *this;

assign(vrRhs);

return *this;
}

Expand Down Expand Up @@ -243,7 +226,7 @@ CMIUtilString::Split(const CMIUtilString &vDelimiter, VecString_t &vwVecSplits)
// 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;
Expand Down Expand Up @@ -299,7 +282,7 @@ CMIUtilString::SplitConsiderQuotes(const CMIUtilString &vDelimiter, VecString_t
// 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;
Expand Down Expand Up @@ -337,7 +320,7 @@ CMIUtilString::StripCREndOfLine() const
if (nPos == std::string::npos)
return *this;

const CMIUtilString strNew(substr(0, nPos).c_str());
const CMIUtilString strNew(substr(0, nPos));

return strNew;
}
Expand Down Expand Up @@ -542,12 +525,12 @@ CMIUtilString::Trim() const
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;
Expand All @@ -568,7 +551,7 @@ CMIUtilString::Trim(const char vChar) const
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;
Expand Down
3 changes: 1 addition & 2 deletions lldb/tools/lldb-mi/MIUtilString.h
Expand Up @@ -43,8 +43,7 @@ class CMIUtilString : public std::string
public:
/* ctor */ CMIUtilString();
/* 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;
Expand Down

0 comments on commit b2b0170

Please sign in to comment.