Added test CheckOther::sizeofsizeof. Inspired by #1682

This commit is contained in:
Daniel Marjamki 2010-05-15 14:06:45 +02:00
parent 85afa86da3
commit 972046c4bd
3 changed files with 44 additions and 6 deletions

View File

@ -3636,3 +3636,21 @@ void CheckOther::fflushOnInputStreamError(const Token *tok, const std::string &v
reportError(tok, Severity::error, reportError(tok, Severity::error,
"fflushOnInputStream", "fflush() called on input stream \"" + varname + "\" may result in undefined behaviour"); "fflushOnInputStream", "fflush() called on input stream \"" + varname + "\" may result in undefined behaviour");
} }
void CheckOther::sizeofsizeof()
{
if (!_settings->_checkCodingStyle)
return;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
if (Token::simpleMatch(tok, "sizeof sizeof"))
sizeofsizeofError(tok);
}
}
void CheckOther::sizeofsizeofError(const Token *tok)
{
reportError(tok, Severity::style,
"sizeofsizeof", "Suspicios code 'sizeof sizeof ..', most likely there should only be one sizeof. The current code is equivalent with 'sizeof(size_t)'.");
}

View File

@ -60,6 +60,7 @@ public:
checkOther.checkVariableScope(); checkOther.checkVariableScope();
checkOther.checkStructMemberUsage(); checkOther.checkStructMemberUsage();
checkOther.strPlusChar(); checkOther.strPlusChar();
checkOther.sizeofsizeof();
} }
/** @brief Run checks against the simplified token list */ /** @brief Run checks against the simplified token list */
@ -162,6 +163,10 @@ public:
/** @brief %Check for using fflush() on an input stream*/ /** @brief %Check for using fflush() on an input stream*/
void checkFflushOnInputStream(); void checkFflushOnInputStream();
/** @brief %Check for 'sizeof sizeof ..' */
void sizeofsizeof();
void sizeofsizeofError(const Token *tok);
// Error messages.. // Error messages..
void cstyleCastError(const Token *tok); void cstyleCastError(const Token *tok);
void redundantIfDelete0Error(const Token *tok); void redundantIfDelete0Error(const Token *tok);
@ -217,6 +222,7 @@ public:
variableScopeError(0, "varname"); variableScopeError(0, "varname");
conditionAlwaysTrueFalse(0, "true/false"); conditionAlwaysTrueFalse(0, "true/false");
strPlusChar(0); strPlusChar(0);
sizeofsizeofError(0);
// optimisations // optimisations
postIncrementError(0, "varname", true); postIncrementError(0, "varname", true);

View File

@ -94,6 +94,8 @@ private:
TEST_CASE(emptyStringTest); TEST_CASE(emptyStringTest);
TEST_CASE(fflushOnInputStreamTest); TEST_CASE(fflushOnInputStreamTest);
TEST_CASE(sizeofsizeof);
} }
void check(const char code[]) void check(const char code[])
@ -103,16 +105,19 @@ private:
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
// Simplify token list..
tokenizer.simplifyTokenList();
// Clear the error buffer..
errout.str("");
// Check.. // Check..
Settings settings; Settings settings;
settings._checkCodingStyle = true; settings._checkCodingStyle = true;
CheckOther checkOther(&tokenizer, &settings, this); CheckOther checkOther(&tokenizer, &settings, this);
// Clear the error buffer..
errout.str("");
checkOther.sizeofsizeof();
// Simplify token list..
tokenizer.simplifyTokenList();
checkOther.warningRedundantCode(); checkOther.warningRedundantCode();
checkOther.checkZeroDivision(); checkOther.checkZeroDivision();
checkOther.checkMathFunctions(); checkOther.checkMathFunctions();
@ -2503,6 +2508,15 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void sizeofsizeof()
{
check("void foo()\n"
"{\n"
" int i = sizeof sizeof char;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Suspicios code 'sizeof sizeof ..', most likely there should only be one sizeof. The current code is equivalent with 'sizeof(size_t)'.\n", errout.str());
}
}; };
REGISTER_TEST(TestOther) REGISTER_TEST(TestOther)