Skip to content

Commit 07c120f

Browse files
committed
Cache option flags and check them first.
1 parent 2e60f30 commit 07c120f

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

lib/checkio.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,9 @@ void CheckIO::seekOnAppendedFileError(const Token *tok)
338338
//---------------------------------------------------------------------------
339339
void CheckIO::invalidScanf()
340340
{
341-
if (!_settings->isEnabled("warning") && !_settings->isEnabled("portability"))
341+
const bool warning = _settings->isEnabled("warning");
342+
const bool portability = _settings->isEnabled("portability");
343+
if (!warning && !portability)
342344
return;
343345

344346
const bool windows = _settings->isWindowsPlatform();
@@ -375,9 +377,9 @@ void CheckIO::invalidScanf()
375377
}
376378

377379
else if (std::isalpha((unsigned char)formatstr[i]) || formatstr[i] == '[') {
378-
if ((formatstr[i] == 's' || formatstr[i] == '[' || formatstr[i] == 'S' || (formatstr[i] == 'l' && formatstr[i+1] == 's')) && _settings->isEnabled("warning")) // #3490 - field width limits are only necessary for string input
380+
if (warning && (formatstr[i] == 's' || formatstr[i] == '[' || formatstr[i] == 'S' || (formatstr[i] == 'l' && formatstr[i+1] == 's'))) // #3490 - field width limits are only necessary for string input
379381
invalidScanfError(tok, false);
380-
else if (formatstr[i] != 'n' && formatstr[i] != 'c' && !windows && _settings->isEnabled("portability"))
382+
else if (portability && formatstr[i] != 'n' && formatstr[i] != 'c' && !windows)
381383
invalidScanfError(tok, true); // Warn about libc bug in versions prior to 2.13-25
382384
format = false;
383385
}
@@ -1322,10 +1324,12 @@ void CheckIO::checkWrongPrintfScanfArguments()
13221324
argListTok2 = argListTok2->nextArgument(); // Find next argument
13231325
}
13241326

1325-
// Check that all parameter positions reference an actual parameter
1326-
for (std::set<unsigned int>::const_iterator it = parameterPositionsUsed.begin() ; it != parameterPositionsUsed.end() ; ++it) {
1327-
if (((*it == 0) || (*it > numFormat)) && _settings->isEnabled("warning"))
1328-
wrongPrintfScanfPosixParameterPositionError(tok, tok->str(), *it, numFormat);
1327+
if (warning) {
1328+
// Check that all parameter positions reference an actual parameter
1329+
for (std::set<unsigned int>::const_iterator it = parameterPositionsUsed.begin() ; it != parameterPositionsUsed.end() ; ++it) {
1330+
if ((*it == 0) || (*it > numFormat))
1331+
wrongPrintfScanfPosixParameterPositionError(tok, tok->str(), *it, numFormat);
1332+
}
13291333
}
13301334

13311335
// Mismatching number of parameters => warning

lib/checkother.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ void CheckOther::checkRedundantAssignment()
798798
memAssignments[param1->varId()] = tok;
799799
else {
800800
const std::map<unsigned int, const Token*>::iterator it = memAssignments.find(param1->varId());
801-
if (scope->type == Scope::eSwitch && Token::findmatch(it->second, "default|case", tok) && warning)
801+
if (warning && scope->type == Scope::eSwitch && Token::findmatch(it->second, "default|case", tok))
802802
redundantCopyInSwitchError(it->second, tok, param1->str());
803803
else if (performance)
804804
redundantCopyError(it->second, tok, param1->str());

lib/checkstl.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -763,8 +763,8 @@ static bool if_findCompare(const Token * const tokBack, bool str)
763763

764764
void CheckStl::if_find()
765765
{
766-
bool warning = _settings->isEnabled("warning");
767-
bool performance = _settings->isEnabled("performance");
766+
const bool warning = _settings->isEnabled("warning");
767+
const bool performance = _settings->isEnabled("performance");
768768
if (!warning && !performance)
769769
return;
770770

@@ -1093,6 +1093,7 @@ static bool isLocal(const Token *tok)
10931093

10941094
void CheckStl::string_c_str()
10951095
{
1096+
const bool performance = _settings->isEnabled("performance");
10961097
// THIS ARRAY MUST BE ORDERED ALPHABETICALLY
10971098
static const char* const stl_string[] = {
10981099
"string", "u16string", "u32string", "wstring"
@@ -1106,7 +1107,7 @@ void CheckStl::string_c_str()
11061107

11071108
// Find all functions that take std::string as argument
11081109
std::multimap<std::string, unsigned int> c_strFuncParam;
1109-
if (_settings->isEnabled("performance")) {
1110+
if (performance) {
11101111
for (std::list<Scope>::const_iterator scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) {
11111112
for (std::list<Function>::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
11121113
if (c_strFuncParam.erase(func->tokenDef->str()) != 0) { // Check if function with this name was already found
@@ -1154,8 +1155,8 @@ void CheckStl::string_c_str()
11541155
const Variable* var = tok->next()->variable();
11551156
if (var && var->isPointer())
11561157
string_c_strError(tok);
1157-
} else if (Token::Match(tok, "%var% ( !!)") && c_strFuncParam.find(tok->str()) != c_strFuncParam.end() &&
1158-
_settings->isEnabled("performance") && !Token::Match(tok->previous(), "::|.") && tok->varId() == 0 && tok->str() != scope->className) { // calling function. TODO: Add support for member functions
1158+
} else if (performance && Token::Match(tok, "%var% ( !!)") && c_strFuncParam.find(tok->str()) != c_strFuncParam.end() &&
1159+
!Token::Match(tok->previous(), "::|.") && tok->varId() == 0 && tok->str() != scope->className) { // calling function. TODO: Add support for member functions
11591160
std::pair<std::multimap<std::string, unsigned int>::const_iterator, std::multimap<std::string, unsigned int>::const_iterator> range = c_strFuncParam.equal_range(tok->str());
11601161
for (std::multimap<std::string, unsigned int>::const_iterator i = range.first; i != range.second; ++i) {
11611162
if (i->second == 0)
@@ -1223,7 +1224,7 @@ void CheckStl::string_c_str()
12231224
}
12241225
}
12251226
// Using c_str() to get the return value is redundant if the function returns std::string or const std::string&.
1226-
else if ((returnType == stdString || returnType == stdStringConstRef) && _settings->isEnabled("performance")) {
1227+
else if (performance && (returnType == stdString || returnType == stdStringConstRef)) {
12271228
if (tok->str() == "return") {
12281229
const Token* tok2 = Token::findsimplematch(tok->next(), ";");
12291230
if (Token::Match(tok2->tokAt(-4), ". c_str|data ( )")) {
@@ -1375,8 +1376,8 @@ void CheckStl::uselessCalls()
13751376
"unordered_set", "vector", "wstring"
13761377
};
13771378

1378-
bool performance = _settings->isEnabled("performance");
1379-
bool warning = _settings->isEnabled("warning");
1379+
const bool performance = _settings->isEnabled("performance");
1380+
const bool warning = _settings->isEnabled("warning");
13801381
if (!performance && !warning)
13811382
return;
13821383

@@ -1385,13 +1386,13 @@ void CheckStl::uselessCalls()
13851386
for (std::size_t i = 0; i < functions; ++i) {
13861387
const Scope * scope = symbolDatabase->functionScopes[i];
13871388
for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) {
1388-
if (tok->varId() && Token::Match(tok, "%var% . compare|find|rfind|find_first_not_of|find_first_of|find_last_not_of|find_last_of ( %var% [,)]") &&
1389-
tok->varId() == tok->tokAt(4)->varId() && warning) {
1389+
if (warning && tok->varId() && Token::Match(tok, "%var% . compare|find|rfind|find_first_not_of|find_first_of|find_last_not_of|find_last_of ( %var% [,)]") &&
1390+
tok->varId() == tok->tokAt(4)->varId()) {
13901391
uselessCallsReturnValueError(tok->tokAt(4), tok->str(), tok->strAt(2));
1391-
} else if (tok->varId() && Token::Match(tok, "%var% . swap ( %var% )") &&
1392-
tok->varId() == tok->tokAt(4)->varId() && performance) {
1392+
} else if (performance && tok->varId() && Token::Match(tok, "%var% . swap ( %var% )") &&
1393+
tok->varId() == tok->tokAt(4)->varId()) {
13931394
uselessCallsSwapError(tok, tok->str());
1394-
} else if (Token::Match(tok, "%var% . substr (") && performance &&
1395+
} else if (performance && Token::Match(tok, "%var% . substr (") &&
13951396
tok->variable() && tok->variable()->isStlType(stl_string)) {
13961397
if (Token::Match(tok->tokAt(4), "0| )"))
13971398
uselessCallsSubstrError(tok, false);
@@ -1400,7 +1401,7 @@ void CheckStl::uselessCalls()
14001401
uselessCallsSubstrError(tok, false);
14011402
} else if (Token::simpleMatch(tok->linkAt(3)->tokAt(-2), ", 0 )"))
14021403
uselessCallsSubstrError(tok, true);
1403-
} else if (Token::Match(tok, "[{};] %var% . empty ( ) ;") && warning &&
1404+
} else if (warning && Token::Match(tok, "[{};] %var% . empty ( ) ;") &&
14041405
tok->next()->variable() && tok->next()->variable()->isStlType(stl_containers_with_empty_and_clear))
14051406
uselessCallsEmptyError(tok->next());
14061407
else if (Token::Match(tok, "[{};] std :: remove|remove_if|unique (") && tok->tokAt(5)->nextArgument())

0 commit comments

Comments
 (0)