Refactorizations:

- Call std::string::find() with char instead of char* where possible
- Avoid string copying
- Optimized several Token::tokAt/strAt calls
This commit is contained in:
PKEuS 2015-08-15 20:24:26 +02:00
parent 1dca7c2162
commit 1627b19dd6
10 changed files with 36 additions and 35 deletions

View File

@ -401,7 +401,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
}
// No "=", append a "=1"
if (define.find("=") == std::string::npos)
if (define.find('=') == std::string::npos)
define += "=1";
if (!_settings->userDefines.empty())

View File

@ -53,7 +53,7 @@ static bool astGetSizeSign(const Settings *settings, const Token *tok, unsigned
return !tok->astOperand2() || astGetSizeSign(settings, tok->astOperand2(), size, sign);
}
if (tok->isNumber() && MathLib::isInt(tok->str())) {
if (tok->str().find("L") != std::string::npos)
if (tok->str().find('L') != std::string::npos)
return false;
MathLib::bigint value = MathLib::toLongNumber(tok->str());
unsigned int sz;

View File

@ -1224,24 +1224,24 @@ void CheckUnusedVar::checkStructMemberUsage()
structname.clear();
if (!structname.empty() && Token::Match(tok, "[{;]")) {
// Declaring struct variable..
std::string varname;
// declaring a POD variable?
if (!tok->next()->isStandardType())
continue;
// Declaring struct variable..
const std::string* varname;
if (Token::Match(tok->next(), "%type% %name% [;[]"))
varname = tok->strAt(2);
varname = &tok->strAt(2);
else if (Token::Match(tok->next(), "%type% %type%|* %name% [;[]"))
varname = tok->strAt(3);
varname = &tok->strAt(3);
else if (Token::Match(tok->next(), "%type% %type% * %name% [;[]"))
varname = tok->strAt(4);
varname = &tok->strAt(4);
else
continue;
// Check if the struct variable is used anywhere in the file
const std::string usagePattern(". " + varname);
const std::string usagePattern(". " + *varname);
bool used = false;
for (const Token *tok2 = _tokenizer->tokens(); tok2; tok2 = tok2->next()) {
if (Token::simpleMatch(tok2, usagePattern.c_str())) {
@ -1251,7 +1251,7 @@ void CheckUnusedVar::checkStructMemberUsage()
}
if (! used) {
unusedStructMemberError(tok->next(), structname, varname);
unusedStructMemberError(tok->next(), structname, *varname);
}
}
}

View File

@ -88,7 +88,7 @@ void ErrorLogger::ErrorMessage::setmsg(const std::string &msg)
// The summary and verbose message are separated by a newline
// If there is no newline then both the summary and verbose messages
// are the given message
const std::string::size_type pos = msg.find("\n");
const std::string::size_type pos = msg.find('\n');
if (pos == std::string::npos) {
_shortMessage = msg;
_verboseMessage = msg;

View File

@ -397,7 +397,7 @@ template<> std::string MathLib::toString(double value)
result << value;
if (result.str() == "-0")
return "0.0";
if (result.str().find(".") == std::string::npos)
if (result.str().find('.') == std::string::npos)
return result.str() + ".0";
return result.str();
}

View File

@ -78,10 +78,10 @@ namespace {
std::string Settings::addEnabled(const std::string &str)
{
// Enable parameters may be comma separated...
if (str.find(",") != std::string::npos) {
if (str.find(',') != std::string::npos) {
std::string::size_type prevPos = 0;
std::string::size_type pos = 0;
while ((pos = str.find(",", pos)) != std::string::npos) {
while ((pos = str.find(',', pos)) != std::string::npos) {
if (pos == prevPos)
return std::string("cppcheck: --enable parameter is empty");
const std::string errmsg(addEnabled(str.substr(prevPos, pos - prevPos)));

View File

@ -66,11 +66,11 @@ std::string Suppressions::addSuppressionLine(const std::string &line)
// is a line number..
// Get position of last colon
const std::string::size_type pos = file.rfind(":");
const std::string::size_type pos = file.rfind(':');
// if a colon is found and there is no dot after it..
if (pos != std::string::npos &&
file.find(".", pos) == std::string::npos) {
file.find('.', pos) == std::string::npos) {
// Try to parse out the line number
try {
std::istringstream istr1(file.substr(pos+1));

View File

@ -3192,8 +3192,8 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const
if (MathLib::isInt(arguments[j]->str())) {
if (arguments[j]->str().find("ll") != std::string::npos ||
arguments[j]->str().find("LL") != std::string::npos) {
if (arguments[j]->str().find("u") != std::string::npos ||
arguments[j]->str().find("U") != std::string::npos) {
if (arguments[j]->str().find('u') != std::string::npos ||
arguments[j]->str().find('U') != std::string::npos) {
if (funcarg->typeStartToken()->str() == "long" &&
funcarg->typeStartToken()->isLong() &&
funcarg->typeStartToken()->isUnsigned()) {
@ -3206,10 +3206,10 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const
same++;
}
}
} else if (arguments[j]->str().find("l") != std::string::npos ||
arguments[j]->str().find("L") != std::string::npos) {
if (arguments[j]->str().find("u") != std::string::npos ||
arguments[j]->str().find("U") != std::string::npos) {
} else if (arguments[j]->str().find('l') != std::string::npos ||
arguments[j]->str().find('L') != std::string::npos) {
if (arguments[j]->str().find('u') != std::string::npos ||
arguments[j]->str().find('U') != std::string::npos) {
if (funcarg->typeStartToken()->str() == "long" &&
!funcarg->typeStartToken()->isLong() &&
funcarg->typeStartToken()->isUnsigned()) {
@ -3222,8 +3222,8 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const
same++;
}
}
} else if (arguments[j]->str().find("u") != std::string::npos ||
arguments[j]->str().find("U") != std::string::npos) {
} else if (arguments[j]->str().find('u') != std::string::npos ||
arguments[j]->str().find('U') != std::string::npos) {
if (funcarg->typeStartToken()->str() == "int" &&
funcarg->typeStartToken()->isUnsigned()) {
same++;
@ -3239,13 +3239,13 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const
}
}
} else {
if (arguments[j]->str().find("f") != std::string::npos ||
arguments[j]->str().find("F") != std::string::npos) {
if (arguments[j]->str().find('f') != std::string::npos ||
arguments[j]->str().find('F') != std::string::npos) {
if (funcarg->typeStartToken()->str() == "float") {
same++;
}
} else if (arguments[j]->str().find("l") != std::string::npos ||
arguments[j]->str().find("L") != std::string::npos) {
} else if (arguments[j]->str().find('l') != std::string::npos ||
arguments[j]->str().find('L') != std::string::npos) {
if (funcarg->typeStartToken()->str() == "double" &&
funcarg->typeStartToken()->isLong()) {
same++;
@ -3344,8 +3344,8 @@ const Function* SymbolDatabase::findFunction(const Token *tok) const
if (currScope) {
while (currScope && !Token::Match(tok1, "%type% :: %any% (")) {
currScope = currScope->findRecordInNestedList(tok1->strAt(2));
tok1 = tok1->tokAt(2);
currScope = currScope->findRecordInNestedList(tok1->str());
}
tok1 = tok1->tokAt(2);

View File

@ -943,7 +943,7 @@ bool TemplateSimplifier::simplifyNumericCalculations(Token *tok)
while (tok->tokAt(4) && tok->next()->isNumber() && tok->tokAt(3)->isNumber()) { // %any% %num% %any% %num% %any%
const Token* op = tok->tokAt(2);
const Token* after = tok->tokAt(4);
if (Token::Match(tok, "* %num% /") && (tok->strAt(3) != "0") && tok->next()->str() == MathLib::multiply(tok->strAt(3), MathLib::divide(tok->next()->str(), tok->strAt(3)))) {
if (Token::Match(tok, "* %num% /") && (op->strAt(1) != "0") && tok->next()->str() == MathLib::multiply(op->strAt(1), MathLib::divide(tok->next()->str(), op->strAt(1)))) {
// Division where result is a whole number
} else if (!((op->str() == "*" && (isLowerThanMulDiv(tok) || tok->str() == "*") && isLowerEqualThanMulDiv(after)) || // associative
(Token::Match(op, "[/%]") && isLowerThanMulDiv(tok) && isLowerEqualThanMulDiv(after)) || // NOT associative

View File

@ -1506,14 +1506,14 @@ void Tokenizer::simplifyTypedef()
}
} else if (typeOf) {
tok2 = copyTokens(tok2, argStart, argEnd);
} else if (tok2->tokAt(2) && tok2->strAt(2) == "[") {
while (tok2->tokAt(2) && tok2->strAt(2) == "[") {
} else if (tok2->strAt(2) == "[") {
do {
if (!tok2->linkAt(2)) {
syntaxError(tok2); // #6807
return;
}
tok2 = tok2->linkAt(2)->previous();
}
} while (tok2->strAt(2) == "[");
}
if (arrayStart && arrayEnd) {
@ -2864,9 +2864,10 @@ void Tokenizer::setVarId()
if (!isC()) {
for (Token *tok2 = list.front(); tok2; tok2 = tok2->next()) {
if (Token::Match(tok2, "%name% :: %name%")) {
if (tok2->strAt(3) == "(")
const std::string& str3 = tok2->strAt(3);
if (str3 == "(")
allMemberFunctions.push_back(tok2);
else if (tok2->strAt(3) != "::" && tok2->strAt(-1) != "::") // Support only one depth
else if (str3 != "::" && tok2->strAt(-1) != "::") // Support only one depth
allMemberVars.push_back(tok2);
}
}