From c26e619b23b63b9ff12a880948975551759c8847 Mon Sep 17 00:00:00 2001 From: Zachary Blair Date: Tue, 20 Apr 2010 23:38:25 -0700 Subject: [PATCH] Fixed #855 (Refactoring: move conditions into checks) --- lib/checkclass.cpp | 18 ++++++++++++++++ lib/checkclass.h | 20 +++++++----------- lib/checkdangerousfunctions.cpp | 3 +++ lib/checkdangerousfunctions.h | 5 +---- lib/checkother.cpp | 32 ++++++++++++++++++++++++++++ lib/checkother.h | 36 +++++++++++++------------------- lib/checkstl.cpp | 3 +++ lib/checkstl.h | 6 ++---- test/testcharvar.cpp | 1 + test/testclass.cpp | 3 +++ test/testdangerousfunctions.cpp | 1 + test/testincompletestatement.cpp | 1 + test/testother.cpp | 5 ++++- test/testunusedvar.cpp | 2 ++ 14 files changed, 94 insertions(+), 42 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 349989a1b..669d63395 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -434,6 +434,9 @@ void CheckClass::initializeVarList(const Token *tok1, const Token *ftok, Var *va void CheckClass::constructors() { + if (!_settings->_checkCodingStyle) + return; + const char pattern_class[] = "class|struct %var% [{:]"; // Locate class @@ -608,6 +611,9 @@ void CheckClass::checkConstructors(const Token *tok1, const std::string &funcnam void CheckClass::privateFunctions() { + if (!_settings->_checkCodingStyle) + return; + // Locate some class for (const Token *tok1 = Token::findmatch(_tokenizer->tokens(), "class|struct %var% {"); tok1; tok1 = Token::findmatch(tok1->next(), "class|struct %var% {")) { @@ -866,6 +872,9 @@ void CheckClass::noMemset() void CheckClass::operatorEq() { + if (!_settings->_checkCodingStyle) + return; + const Token *tok2 = _tokenizer->tokens(); const Token *tok; @@ -960,6 +969,9 @@ void CheckClass::operatorEqRetRefThis_finderr(const Token *tok, const std::strin void CheckClass::operatorEqRetRefThis() { + if (!_settings->_checkCodingStyle) + return; + const Token *tok2 = _tokenizer->tokens(); const Token *tok; @@ -1204,6 +1216,9 @@ static bool hasMultipleInheritanceGlobal(const Token * start, const std::string void CheckClass::operatorEqToSelf() { + if (!_settings->_checkCodingStyle || !_settings->inconclusive) + return; + const Token *tok2 = _tokenizer->tokens(); const Token *tok; @@ -1464,6 +1479,9 @@ void CheckClass::thisSubtractionError(const Token *tok) void CheckClass::thisSubtraction() { + if (!_settings->_checkCodingStyle || !_settings->inconclusive) + return; + const Token *tok = _tokenizer->tokens(); for (;;) { diff --git a/lib/checkclass.h b/lib/checkclass.h index f95b98d30..5d4e83309 100644 --- a/lib/checkclass.h +++ b/lib/checkclass.h @@ -57,18 +57,14 @@ public: { CheckClass checkClass(tokenizer, settings, errorLogger); - if (settings->_checkCodingStyle) - { - checkClass.constructors(); - checkClass.operatorEq(); - checkClass.privateFunctions(); - checkClass.operatorEqRetRefThis(); - if (settings->inconclusive) - { - checkClass.thisSubtraction(); - checkClass.operatorEqToSelf(); - } - } + // Coding style checks + checkClass.constructors(); + checkClass.operatorEq(); + checkClass.privateFunctions(); + checkClass.operatorEqRetRefThis(); + checkClass.thisSubtraction(); + checkClass.operatorEqToSelf(); + checkClass.virtualDestructor(); checkClass.checkConst(); } diff --git a/lib/checkdangerousfunctions.cpp b/lib/checkdangerousfunctions.cpp index 0250c3fc6..e1f7d5447 100644 --- a/lib/checkdangerousfunctions.cpp +++ b/lib/checkdangerousfunctions.cpp @@ -33,6 +33,9 @@ CheckDangerousFunctions instance; void CheckDangerousFunctions::dangerousFunctions() { + if (!_settings->_checkCodingStyle) + return; + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { if (Token::simpleMatch(tok, "mktemp (")) diff --git a/lib/checkdangerousfunctions.h b/lib/checkdangerousfunctions.h index bb42f6bca..b164b43b7 100644 --- a/lib/checkdangerousfunctions.h +++ b/lib/checkdangerousfunctions.h @@ -46,10 +46,7 @@ public: void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) { CheckDangerousFunctions checkDangerousFunctions(tokenizer, settings, errorLogger); - if (settings->_checkCodingStyle) - { - checkDangerousFunctions.dangerousFunctions(); - } + checkDangerousFunctions.dangerousFunctions(); } /** Check for dangerous functions */ diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 9f6ef0204..96c815c89 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -45,6 +45,9 @@ CheckOther instance; void CheckOther::warningOldStylePointerCast() { + if (!_settings->_checkCodingStyle) + return; + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { // Old style pointer casting.. @@ -77,6 +80,8 @@ void CheckOther::warningOldStylePointerCast() void CheckOther::warningRedundantCode() { + if (!_settings->_checkCodingStyle) + return; // if (p) delete p for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) @@ -219,6 +224,9 @@ void CheckOther::redundantCondition2() //--------------------------------------------------------------------------- void CheckOther::checkEmptyStringTest() { + if (!_settings->_checkCodingStyle) + return; + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { // Non-empty string tests @@ -403,6 +411,9 @@ void CheckOther::checkUnsignedDivision() void CheckOther::unreachableCode() { + if (!_settings->_checkCodingStyle) + return; + const Token *tok = _tokenizer->tokens(); while ((tok = Token::findmatch(tok, "[;{}] return")) != NULL) { @@ -495,6 +506,9 @@ public: void CheckOther::functionVariableUsage() { + if (!_settings->_checkCodingStyle) + return; + // Parse all executing scopes.. for (const Token *token = Token::findmatch(_tokenizer->tokens(), ") const| {"); token;) { @@ -745,6 +759,9 @@ void CheckOther::unassignedVariableError(const Token *tok, const std::string &va void CheckOther::checkVariableScope() { + if (!_settings->_checkCodingStyle) + return; + // Walk through all tokens.. bool func = false; int indentlevel = 0; @@ -919,6 +936,9 @@ void CheckOther::lookupVar(const Token *tok1, const std::string &varname) void CheckOther::checkConstantFunctionParameter() { + if (!_settings->_checkCodingStyle) + return; + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { if (Token::Match(tok, "[,(] const std :: %type% %var% [,)]")) @@ -976,6 +996,9 @@ void CheckOther::checkConstantFunctionParameter() void CheckOther::checkStructMemberUsage() { + if (!_settings->_checkCodingStyle) + return; + std::string structname; for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { @@ -1065,6 +1088,9 @@ void CheckOther::checkStructMemberUsage() void CheckOther::checkCharVariable() { + if (!_settings->_checkCodingStyle) + return; + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { // Declaring the variable.. @@ -1143,6 +1169,9 @@ void CheckOther::checkCharVariable() void CheckOther::checkIncompleteStatement() { + if (!_settings->_checkCodingStyle) + return; + int parlevel = 0; for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) @@ -2754,6 +2783,9 @@ void CheckOther::checkMathFunctions() void CheckOther::postIncrement() { + if (!_settings->_checkCodingStyle || !_settings->inconclusive) + return; + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { if (Token::simpleMatch(tok, "for (")) diff --git a/lib/checkother.h b/lib/checkother.h index f555f624a..05dc22dc2 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -51,15 +51,14 @@ public: CheckOther checkOther(tokenizer, settings, errorLogger); checkOther.nullPointer(); - if (settings->_checkCodingStyle) - { - checkOther.warningOldStylePointerCast(); - checkOther.checkUnsignedDivision(); - checkOther.checkCharVariable(); - checkOther.functionVariableUsage(); - checkOther.checkVariableScope(); - checkOther.checkStructMemberUsage(); - } + + // Coding style checks + checkOther.warningOldStylePointerCast(); + checkOther.checkUnsignedDivision(); + checkOther.checkCharVariable(); + checkOther.functionVariableUsage(); + checkOther.checkVariableScope(); + checkOther.checkStructMemberUsage(); } /** @brief Run checks against the simplified token list */ @@ -67,18 +66,13 @@ public: { CheckOther checkOther(tokenizer, settings, errorLogger); - if (settings->_checkCodingStyle) - { - checkOther.warningRedundantCode(); - checkOther.checkConstantFunctionParameter(); - checkOther.checkIncompleteStatement(); - checkOther.unreachableCode(); - checkOther.checkEmptyStringTest(); - if (settings->inconclusive) - { - checkOther.postIncrement(); - } - } + // Coding style checks + checkOther.warningRedundantCode(); + checkOther.checkConstantFunctionParameter(); + checkOther.checkIncompleteStatement(); + checkOther.unreachableCode(); + checkOther.checkEmptyStringTest(); + checkOther.postIncrement(); checkOther.strPlusChar(); checkOther.invalidFunctionUsage(); diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index c6a113c6e..b2de1b887 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -619,6 +619,9 @@ bool CheckStl::isStlContainer(const Token *tok) void CheckStl::size() { + if (!_settings->_checkCodingStyle || !_settings->inconclusive) + return; + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { if (Token::Match(tok, "%var% . size ( )")) diff --git a/lib/checkstl.h b/lib/checkstl.h index 5ffe907d4..b1a1e32c1 100644 --- a/lib/checkstl.h +++ b/lib/checkstl.h @@ -55,10 +55,8 @@ public: checkStl.stlBoundries(); checkStl.if_find(); - if (settings->_checkCodingStyle && settings->inconclusive) - { - checkStl.size(); - } + // Style check + checkStl.size(); } diff --git a/test/testcharvar.cpp b/test/testcharvar.cpp index bd6013437..6e0ee19d2 100644 --- a/test/testcharvar.cpp +++ b/test/testcharvar.cpp @@ -56,6 +56,7 @@ private: // Check char variable usage.. Settings settings; + settings._checkCodingStyle = true; CheckOther checkOther(&tokenizer, &settings, this); checkOther.checkCharVariable(); } diff --git a/test/testclass.cpp b/test/testclass.cpp index 98172f2eb..db7cfb717 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -138,6 +138,7 @@ private: // Check.. Settings settings; + settings._checkCodingStyle = true; CheckClass checkClass(&tokenizer, &settings, this); checkClass.operatorEq(); } @@ -1397,6 +1398,7 @@ private: // Check.. Settings settings; settings.inconclusive = true; + settings._checkCodingStyle = true; CheckClass checkClass(&tokenizer, &settings, this); checkClass.constructors(); } @@ -2113,6 +2115,7 @@ private: // Check.. Settings settings; settings._checkCodingStyle = true; + settings.inconclusive = true; CheckClass checkClass(&tokenizer, &settings, this); checkClass.thisSubtraction(); } diff --git a/test/testdangerousfunctions.cpp b/test/testdangerousfunctions.cpp index d9d344ba6..8181f03c4 100644 --- a/test/testdangerousfunctions.cpp +++ b/test/testdangerousfunctions.cpp @@ -54,6 +54,7 @@ private: // Check for dangerous functions.. Settings settings; + settings._checkCodingStyle = true; settings.inconclusive = true; CheckDangerousFunctions checkDangerousFunctions(&tokenizer, &settings, this); checkDangerousFunctions.dangerousFunctions(); diff --git a/test/testincompletestatement.cpp b/test/testincompletestatement.cpp index 5421bb5a9..0918b5656 100644 --- a/test/testincompletestatement.cpp +++ b/test/testincompletestatement.cpp @@ -49,6 +49,7 @@ private: // Check for incomplete statements.. Settings settings; + settings._checkCodingStyle = true; CheckOther checkOther(&tokenizer, &settings, this); checkOther.checkIncompleteStatement(); } diff --git a/test/testother.cpp b/test/testother.cpp index bb01c35ef..96a5f9ed9 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -109,8 +109,9 @@ private: // Clear the error buffer.. errout.str(""); - // Check for redundant code.. + // Check.. Settings settings; + settings._checkCodingStyle = true; CheckOther checkOther(&tokenizer, &settings, this); checkOther.warningRedundantCode(); checkOther.checkZeroDivision(); @@ -2051,6 +2052,7 @@ private: // Check for redundant code.. Settings settings; settings._checkCodingStyle = true; + settings.inconclusive = true; CheckOther checkOther(&tokenizer, &settings, this); checkOther.postIncrement(); } @@ -2164,6 +2166,7 @@ private: errout.str(""); Settings settings; + settings._checkCodingStyle = true; CheckOther checkOther(&tokenizer, &settings, this); checkOther.checkConstantFunctionParameter(); } diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 05d7d8d5d..5e8937592 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -46,6 +46,7 @@ private: // Check for unused variables.. Settings settings; + settings._checkCodingStyle = true; CheckOther checkOther(&tokenizer, &settings, this); checkOther.checkStructMemberUsage(); } @@ -310,6 +311,7 @@ private: // Check for unused variables.. Settings settings; + settings._checkCodingStyle = true; CheckOther checkOther(&tokenizer, &settings, this); checkOther.functionVariableUsage(); }