diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index cd5836e80..dcc644fd7 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -118,8 +118,8 @@ static bool variableIsUsedInScope(const Token* start, unsigned int varId, const void CheckAutoVariables::assignFunctionArg() { - bool style = _settings->isEnabled("style"); - bool warning = _settings->isEnabled("warning"); + const bool style = _settings->isEnabled("style"); + const bool warning = _settings->isEnabled("warning"); if (!style && !warning) return; diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index a934395b4..f4ccc23c6 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -979,6 +979,8 @@ static const Scope* findFunctionOf(const Scope* scope) void CheckClass::checkMemset() { + const bool printWarnings = _settings->isEnabled("warning"); + const std::size_t functions = symbolDatabase->functionScopes.size(); for (std::size_t i = 0; i < functions; ++i) { const Scope * scope = symbolDatabase->functionScopes[i]; @@ -1052,7 +1054,7 @@ void CheckClass::checkMemset() std::list parsedTypes; checkMemsetType(scope, tok->tokAt(2), tok->variable()->typeScope(), true, parsedTypes); - if (tok->variable()->typeScope()->numConstructors > 0 && _settings->isEnabled("warning")) + if (tok->variable()->typeScope()->numConstructors > 0 && printWarnings) mallocOnClassWarning(tok, tok->strAt(2), tok->variable()->typeScope()->classDef); } } @@ -1061,6 +1063,8 @@ void CheckClass::checkMemset() void CheckClass::checkMemsetType(const Scope *start, const Token *tok, const Scope *type, bool allocation, std::list parsedTypes) { + const bool printPortability = _settings->isEnabled("portability"); + // If type has been checked there is no need to check it again if (std::find(parsedTypes.begin(), parsedTypes.end(), type) != parsedTypes.end()) return; @@ -1110,7 +1114,7 @@ void CheckClass::checkMemsetType(const Scope *start, const Token *tok, const Sco checkMemsetType(start, tok, typeScope, allocation, parsedTypes); // check for float - else if (tok->str() == "memset" && var->isFloatingType() && _settings->isEnabled("portability")) + else if (tok->str() == "memset" && var->isFloatingType() && printPortability) memsetErrorFloat(tok, type->classDef->str()); } } diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index e8c68706c..4edb71b9d 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -584,8 +584,8 @@ template static T getvalue(const int test, const T value1, const T valu void CheckCondition::checkIncorrectLogicOperator() { - bool style = _settings->isEnabled("style"); - bool warning = _settings->isEnabled("warning"); + const bool style = _settings->isEnabled("style"); + const bool warning = _settings->isEnabled("warning"); if (!style && !warning) return; diff --git a/lib/checkio.cpp b/lib/checkio.cpp index 8886f8226..86050cb8f 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -100,6 +100,8 @@ void CheckIO::checkFileUsage() }; static const std::set whitelist(_whitelist, _whitelist + sizeof(_whitelist)/sizeof(*_whitelist)); const bool windows = _settings->isWindowsPlatform(); + const bool printPortability = _settings->isEnabled("portability"); + const bool printWarnings = _settings->isEnabled("warning"); std::map filepointers; @@ -179,7 +181,7 @@ void CheckIO::checkFileUsage() operation = Filepointer::OPEN; } else if ((tok->str() == "rewind" || tok->str() == "fseek" || tok->str() == "fsetpos" || tok->str() == "fflush") || (windows && tok->str() == "_fseeki64")) { - if (_settings->isEnabled("portability") && tok->str() == "fflush") { + if (printPortability && tok->str() == "fflush") { fileTok = tok->tokAt(2); if (fileTok) { if (fileTok->str() == "stdin") @@ -270,7 +272,7 @@ void CheckIO::checkFileUsage() case Filepointer::POSITIONING: if (f.mode == CLOSED) useClosedFileError(tok); - else if (f.append_mode == Filepointer::APPEND && tok->str() != "fflush" && _settings->isEnabled("warning")) + else if (f.append_mode == Filepointer::APPEND && tok->str() != "fflush" && printWarnings) seekOnAppendedFileError(tok); break; case Filepointer::READ: diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 88185a87b..b3bab8c3e 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -308,6 +308,8 @@ void CheckNullPointer::nullPointerLinkedList() void CheckNullPointer::nullPointerByDeRefAndChec() { + const bool printWarnings = _settings->isEnabled("warning"); + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { const Variable *var = tok->variable(); if (!var || !var->isPointer() || tok == var->nameToken()) @@ -336,7 +338,7 @@ void CheckNullPointer::nullPointerByDeRefAndChec() if (std::find(varlist.begin(), varlist.end(), tok) != varlist.end()) { if (value->condition == nullptr) nullPointerError(tok, tok->str(), false, value->defaultArg); - else if (_settings->isEnabled("warning")) + else if (printWarnings) nullPointerError(tok, tok->str(), value->condition, value->inconclusive); } continue; @@ -356,7 +358,7 @@ void CheckNullPointer::nullPointerByDeRefAndChec() if (value->condition == nullptr) nullPointerError(tok, tok->str(), value->inconclusive, value->defaultArg); - else if (_settings->isEnabled("warning")) + else if (printWarnings) nullPointerError(tok, tok->str(), value->condition, value->inconclusive); } } diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 7906229dc..3ef8d65b7 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1785,6 +1785,8 @@ void CheckOther::constStatementError(const Token *tok, const std::string &type) //--------------------------------------------------------------------------- void CheckOther::checkZeroDivision() { + const bool printWarnings = _settings->isEnabled("warning"); + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { if (Token::Match(tok, "div|ldiv|lldiv|imaxdiv ( %num% , %num% )") && MathLib::isInt(tok->strAt(4)) && @@ -1804,7 +1806,7 @@ void CheckOther::checkZeroDivision() continue; if (value->condition == nullptr) zerodivError(tok, value->inconclusive); - else if (_settings->isEnabled("warning")) + else if (printWarnings) zerodivcondError(value->condition,tok,value->inconclusive); } } @@ -1872,7 +1874,7 @@ void CheckOther::nanInArithmeticExpressionError(const Token *tok) void CheckOther::checkMathFunctions() { bool styleC99 = _settings->isEnabled("style") && _settings->standards.c != Standards::C89 && _settings->standards.cpp != Standards::CPP03; - bool printWarnings = _settings->isEnabled("warning"); + const bool printWarnings = _settings->isEnabled("warning"); const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); const std::size_t functions = symbolDatabase->functionScopes.size(); @@ -2553,8 +2555,8 @@ void CheckOther::checkIncompleteArrayFill() { if (!_settings->inconclusive) return; - bool warning = _settings->isEnabled("warning"); - bool portability = _settings->isEnabled("portability"); + const bool warning = _settings->isEnabled("warning"); + const bool portability = _settings->isEnabled("portability"); if (!portability && !warning) return; diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index d0afcab88..a6cbbb513 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -1238,6 +1238,7 @@ void CheckStl::checkAutoPointer() std::map mallocVarId; // variables allocated by the malloc-like function static const char STL_CONTAINER_LIST[] = "array|bitset|deque|list|forward_list|map|multimap|multiset|priority_queue|queue|set|stack|vector|hash_map|hash_multimap|hash_set|unordered_map|unordered_multimap|unordered_set|unordered_multiset|basic_string"; const int malloc = _settings->library.alloc("malloc"); // allocation function, which are not compatible with auto_ptr + const bool printStyle = _settings->isEnabled("style"); for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { if (Token::simpleMatch(tok, "auto_ptr <")) { @@ -1284,7 +1285,7 @@ void CheckStl::checkAutoPointer() } } else { if (Token::Match(tok, "%name% = %var% ;")) { - if (_settings->isEnabled("style")) { + if (printStyle) { std::set::const_iterator iter = autoPtrVarId.find(tok->tokAt(2)->varId()); if (iter != autoPtrVarId.end()) { autoPointerError(tok->tokAt(2)); diff --git a/lib/checktype.cpp b/lib/checktype.cpp index a5921fcd5..639464893 100644 --- a/lib/checktype.cpp +++ b/lib/checktype.cpp @@ -99,6 +99,8 @@ static bool astGetSizeSign(const Settings *settings, const Token *tok, unsigned void CheckType::checkTooBigBitwiseShift() { + const bool printWarnings = _settings->isEnabled("warning"); + // unknown sizeof(int) => can't run this checker if (_settings->platformType == Settings::Unspecified) return; @@ -134,7 +136,7 @@ void CheckType::checkTooBigBitwiseShift() const ValueFlow::Value *value = tok->astOperand2()->getValueGE(lhsbits, _settings); if (!value) continue; - if (value->condition && !_settings->isEnabled("warning")) + if (value->condition && !printWarnings) continue; if (value->inconclusive && !_settings->inconclusive) continue; diff --git a/lib/checkvaarg.cpp b/lib/checkvaarg.cpp index 23859b7df..4b8f8b2d7 100644 --- a/lib/checkvaarg.cpp +++ b/lib/checkvaarg.cpp @@ -35,6 +35,8 @@ void CheckVaarg::va_start_argument() { const SymbolDatabase* const symbolDatabase = _tokenizer->getSymbolDatabase(); const std::size_t functions = symbolDatabase->functionScopes.size(); + const bool printWarnings = _settings->isEnabled("warning"); + for (std::size_t i = 0; i < functions; ++i) { const Scope* scope = symbolDatabase->functionScopes[i]; const Function* function = scope->function; @@ -48,7 +50,7 @@ void CheckVaarg::va_start_argument() const Variable* var = param2->variable(); if (var && var->isReference()) referenceAs_va_start_error(param2, var->name()); - if (var && var->index() + 2 < function->argCount() && _settings->isEnabled("warning")) { + if (var && var->index() + 2 < function->argCount() && printWarnings) { std::list::const_reverse_iterator it = function->argumentList.rbegin(); ++it; wrongParameterTo_va_start_error(tok, var->name(), it->name()); diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 36fd044bf..fcd9381d5 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -2870,6 +2870,8 @@ static bool getlines(std::istream &istr, std::string &line) bool Preprocessor::validateCfg(const std::string &code, const std::string &cfg) { + const bool printInformation = (_settings && _settings->isEnabled("information")); + // fill up "macros" with empty configuration macros std::set macros; for (std::string::size_type pos = 0; pos < cfg.size();) { @@ -2917,7 +2919,7 @@ bool Preprocessor::validateCfg(const std::string &code, const std::string &cfg) if (pos2 < code.size() && (std::isalnum((unsigned char)code[pos2]) || code[pos2] == '_')) continue; // macro is used in code, return false - if (_settings && _settings->isEnabled("information")) + if (printInformation) validateCfgError(cfg, macro); return false; } diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index ed427b357..911dd9902 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7515,6 +7515,7 @@ void Tokenizer::simplifyEnum() std::string className; int classLevel = 0; bool goback = false; + const bool printStyle = _settings->isEnabled("style"); for (Token *tok = list.front(); tok; tok = tok->next()) { if (goback) { @@ -7776,7 +7777,7 @@ void Tokenizer::simplifyEnum() if (prev->str() == "(" && (!Token::Match(prev->tokAt(-2), "%type%|::|*|& %type% (") || prev->strAt(-2) == "else")) continue; shadowVars.insert(arg->str()); - if (inScope && _settings->isEnabled("style")) { + if (inScope && printStyle) { const EnumValue& enumValue = enumValues.find(arg->str())->second; duplicateEnumError(arg, enumValue.name, "Function argument"); } @@ -7796,7 +7797,7 @@ void Tokenizer::simplifyEnum() (Token::Match(prev->previous(), "%type% *|&") && (prev->previous()->isStandardType() || prev->strAt(-1) == "const" || Token::Match(prev->tokAt(-2), ";|{|}")))) { // variable declaration? shadowVars.insert(tok3->str()); - if (inScope && _settings->isEnabled("style")) { + if (inScope && printStyle) { const EnumValue& enumValue = enumValues.find(tok3->str())->second; duplicateEnumError(tok3, enumValue.name, "Variable"); }