Skip to content

Commit 1627b19

Browse files
committed
Refactorizations:
- Call std::string::find() with char instead of char* where possible - Avoid string copying - Optimized several Token::tokAt/strAt calls
1 parent 1dca7c2 commit 1627b19

10 files changed

Lines changed: 36 additions & 35 deletions

cli/cmdlineparser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
401401
}
402402

403403
// No "=", append a "=1"
404-
if (define.find("=") == std::string::npos)
404+
if (define.find('=') == std::string::npos)
405405
define += "=1";
406406

407407
if (!_settings->userDefines.empty())

lib/checktype.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static bool astGetSizeSign(const Settings *settings, const Token *tok, unsigned
5353
return !tok->astOperand2() || astGetSizeSign(settings, tok->astOperand2(), size, sign);
5454
}
5555
if (tok->isNumber() && MathLib::isInt(tok->str())) {
56-
if (tok->str().find("L") != std::string::npos)
56+
if (tok->str().find('L') != std::string::npos)
5757
return false;
5858
MathLib::bigint value = MathLib::toLongNumber(tok->str());
5959
unsigned int sz;

lib/checkunusedvar.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,24 +1224,24 @@ void CheckUnusedVar::checkStructMemberUsage()
12241224
structname.clear();
12251225

12261226
if (!structname.empty() && Token::Match(tok, "[{;]")) {
1227-
// Declaring struct variable..
1228-
std::string varname;
1229-
12301227
// declaring a POD variable?
12311228
if (!tok->next()->isStandardType())
12321229
continue;
12331230

1231+
// Declaring struct variable..
1232+
const std::string* varname;
1233+
12341234
if (Token::Match(tok->next(), "%type% %name% [;[]"))
1235-
varname = tok->strAt(2);
1235+
varname = &tok->strAt(2);
12361236
else if (Token::Match(tok->next(), "%type% %type%|* %name% [;[]"))
1237-
varname = tok->strAt(3);
1237+
varname = &tok->strAt(3);
12381238
else if (Token::Match(tok->next(), "%type% %type% * %name% [;[]"))
1239-
varname = tok->strAt(4);
1239+
varname = &tok->strAt(4);
12401240
else
12411241
continue;
12421242

12431243
// Check if the struct variable is used anywhere in the file
1244-
const std::string usagePattern(". " + varname);
1244+
const std::string usagePattern(". " + *varname);
12451245
bool used = false;
12461246
for (const Token *tok2 = _tokenizer->tokens(); tok2; tok2 = tok2->next()) {
12471247
if (Token::simpleMatch(tok2, usagePattern.c_str())) {
@@ -1251,7 +1251,7 @@ void CheckUnusedVar::checkStructMemberUsage()
12511251
}
12521252

12531253
if (! used) {
1254-
unusedStructMemberError(tok->next(), structname, varname);
1254+
unusedStructMemberError(tok->next(), structname, *varname);
12551255
}
12561256
}
12571257
}

lib/errorlogger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void ErrorLogger::ErrorMessage::setmsg(const std::string &msg)
8888
// The summary and verbose message are separated by a newline
8989
// If there is no newline then both the summary and verbose messages
9090
// are the given message
91-
const std::string::size_type pos = msg.find("\n");
91+
const std::string::size_type pos = msg.find('\n');
9292
if (pos == std::string::npos) {
9393
_shortMessage = msg;
9494
_verboseMessage = msg;

lib/mathlib.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ template<> std::string MathLib::toString(double value)
397397
result << value;
398398
if (result.str() == "-0")
399399
return "0.0";
400-
if (result.str().find(".") == std::string::npos)
400+
if (result.str().find('.') == std::string::npos)
401401
return result.str() + ".0";
402402
return result.str();
403403
}

lib/settings.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ namespace {
7878
std::string Settings::addEnabled(const std::string &str)
7979
{
8080
// Enable parameters may be comma separated...
81-
if (str.find(",") != std::string::npos) {
81+
if (str.find(',') != std::string::npos) {
8282
std::string::size_type prevPos = 0;
8383
std::string::size_type pos = 0;
84-
while ((pos = str.find(",", pos)) != std::string::npos) {
84+
while ((pos = str.find(',', pos)) != std::string::npos) {
8585
if (pos == prevPos)
8686
return std::string("cppcheck: --enable parameter is empty");
8787
const std::string errmsg(addEnabled(str.substr(prevPos, pos - prevPos)));

lib/suppressions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ std::string Suppressions::addSuppressionLine(const std::string &line)
6666
// is a line number..
6767

6868
// Get position of last colon
69-
const std::string::size_type pos = file.rfind(":");
69+
const std::string::size_type pos = file.rfind(':');
7070

7171
// if a colon is found and there is no dot after it..
7272
if (pos != std::string::npos &&
73-
file.find(".", pos) == std::string::npos) {
73+
file.find('.', pos) == std::string::npos) {
7474
// Try to parse out the line number
7575
try {
7676
std::istringstream istr1(file.substr(pos+1));

lib/symboldatabase.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3192,8 +3192,8 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const
31923192
if (MathLib::isInt(arguments[j]->str())) {
31933193
if (arguments[j]->str().find("ll") != std::string::npos ||
31943194
arguments[j]->str().find("LL") != std::string::npos) {
3195-
if (arguments[j]->str().find("u") != std::string::npos ||
3196-
arguments[j]->str().find("U") != std::string::npos) {
3195+
if (arguments[j]->str().find('u') != std::string::npos ||
3196+
arguments[j]->str().find('U') != std::string::npos) {
31973197
if (funcarg->typeStartToken()->str() == "long" &&
31983198
funcarg->typeStartToken()->isLong() &&
31993199
funcarg->typeStartToken()->isUnsigned()) {
@@ -3206,10 +3206,10 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const
32063206
same++;
32073207
}
32083208
}
3209-
} else if (arguments[j]->str().find("l") != std::string::npos ||
3210-
arguments[j]->str().find("L") != std::string::npos) {
3211-
if (arguments[j]->str().find("u") != std::string::npos ||
3212-
arguments[j]->str().find("U") != std::string::npos) {
3209+
} else if (arguments[j]->str().find('l') != std::string::npos ||
3210+
arguments[j]->str().find('L') != std::string::npos) {
3211+
if (arguments[j]->str().find('u') != std::string::npos ||
3212+
arguments[j]->str().find('U') != std::string::npos) {
32133213
if (funcarg->typeStartToken()->str() == "long" &&
32143214
!funcarg->typeStartToken()->isLong() &&
32153215
funcarg->typeStartToken()->isUnsigned()) {
@@ -3222,8 +3222,8 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const
32223222
same++;
32233223
}
32243224
}
3225-
} else if (arguments[j]->str().find("u") != std::string::npos ||
3226-
arguments[j]->str().find("U") != std::string::npos) {
3225+
} else if (arguments[j]->str().find('u') != std::string::npos ||
3226+
arguments[j]->str().find('U') != std::string::npos) {
32273227
if (funcarg->typeStartToken()->str() == "int" &&
32283228
funcarg->typeStartToken()->isUnsigned()) {
32293229
same++;
@@ -3239,13 +3239,13 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const
32393239
}
32403240
}
32413241
} else {
3242-
if (arguments[j]->str().find("f") != std::string::npos ||
3243-
arguments[j]->str().find("F") != std::string::npos) {
3242+
if (arguments[j]->str().find('f') != std::string::npos ||
3243+
arguments[j]->str().find('F') != std::string::npos) {
32443244
if (funcarg->typeStartToken()->str() == "float") {
32453245
same++;
32463246
}
3247-
} else if (arguments[j]->str().find("l") != std::string::npos ||
3248-
arguments[j]->str().find("L") != std::string::npos) {
3247+
} else if (arguments[j]->str().find('l') != std::string::npos ||
3248+
arguments[j]->str().find('L') != std::string::npos) {
32493249
if (funcarg->typeStartToken()->str() == "double" &&
32503250
funcarg->typeStartToken()->isLong()) {
32513251
same++;
@@ -3344,8 +3344,8 @@ const Function* SymbolDatabase::findFunction(const Token *tok) const
33443344

33453345
if (currScope) {
33463346
while (currScope && !Token::Match(tok1, "%type% :: %any% (")) {
3347-
currScope = currScope->findRecordInNestedList(tok1->strAt(2));
33483347
tok1 = tok1->tokAt(2);
3348+
currScope = currScope->findRecordInNestedList(tok1->str());
33493349
}
33503350

33513351
tok1 = tok1->tokAt(2);

lib/templatesimplifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ bool TemplateSimplifier::simplifyNumericCalculations(Token *tok)
943943
while (tok->tokAt(4) && tok->next()->isNumber() && tok->tokAt(3)->isNumber()) { // %any% %num% %any% %num% %any%
944944
const Token* op = tok->tokAt(2);
945945
const Token* after = tok->tokAt(4);
946-
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)))) {
946+
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)))) {
947947
// Division where result is a whole number
948948
} else if (!((op->str() == "*" && (isLowerThanMulDiv(tok) || tok->str() == "*") && isLowerEqualThanMulDiv(after)) || // associative
949949
(Token::Match(op, "[/%]") && isLowerThanMulDiv(tok) && isLowerEqualThanMulDiv(after)) || // NOT associative

lib/tokenize.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,14 +1506,14 @@ void Tokenizer::simplifyTypedef()
15061506
}
15071507
} else if (typeOf) {
15081508
tok2 = copyTokens(tok2, argStart, argEnd);
1509-
} else if (tok2->tokAt(2) && tok2->strAt(2) == "[") {
1510-
while (tok2->tokAt(2) && tok2->strAt(2) == "[") {
1509+
} else if (tok2->strAt(2) == "[") {
1510+
do {
15111511
if (!tok2->linkAt(2)) {
15121512
syntaxError(tok2); // #6807
15131513
return;
15141514
}
15151515
tok2 = tok2->linkAt(2)->previous();
1516-
}
1516+
} while (tok2->strAt(2) == "[");
15171517
}
15181518

15191519
if (arrayStart && arrayEnd) {
@@ -2864,9 +2864,10 @@ void Tokenizer::setVarId()
28642864
if (!isC()) {
28652865
for (Token *tok2 = list.front(); tok2; tok2 = tok2->next()) {
28662866
if (Token::Match(tok2, "%name% :: %name%")) {
2867-
if (tok2->strAt(3) == "(")
2867+
const std::string& str3 = tok2->strAt(3);
2868+
if (str3 == "(")
28682869
allMemberFunctions.push_back(tok2);
2869-
else if (tok2->strAt(3) != "::" && tok2->strAt(-1) != "::") // Support only one depth
2870+
else if (str3 != "::" && tok2->strAt(-1) != "::") // Support only one depth
28702871
allMemberVars.push_back(tok2);
28712872
}
28722873
}

0 commit comments

Comments
 (0)