From 972046c4bd7974c40db633318a94d624ff8517d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=E4ki?= Date: Sat, 15 May 2010 14:06:45 +0200 Subject: [PATCH] Added test CheckOther::sizeofsizeof. Inspired by #1682 --- lib/checkother.cpp | 18 ++++++++++++++++++ lib/checkother.h | 6 ++++++ test/testother.cpp | 26 ++++++++++++++++++++------ 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index a18c32110..3676f60fa 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -3636,3 +3636,21 @@ void CheckOther::fflushOnInputStreamError(const Token *tok, const std::string &v reportError(tok, Severity::error, "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)'."); +} diff --git a/lib/checkother.h b/lib/checkother.h index 67ad25821..0a1208206 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -60,6 +60,7 @@ public: checkOther.checkVariableScope(); checkOther.checkStructMemberUsage(); checkOther.strPlusChar(); + checkOther.sizeofsizeof(); } /** @brief Run checks against the simplified token list */ @@ -162,6 +163,10 @@ public: /** @brief %Check for using fflush() on an input stream*/ void checkFflushOnInputStream(); + /** @brief %Check for 'sizeof sizeof ..' */ + void sizeofsizeof(); + void sizeofsizeofError(const Token *tok); + // Error messages.. void cstyleCastError(const Token *tok); void redundantIfDelete0Error(const Token *tok); @@ -217,6 +222,7 @@ public: variableScopeError(0, "varname"); conditionAlwaysTrueFalse(0, "true/false"); strPlusChar(0); + sizeofsizeofError(0); // optimisations postIncrementError(0, "varname", true); diff --git a/test/testother.cpp b/test/testother.cpp index 9ee552fd6..03dedb934 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -94,6 +94,8 @@ private: TEST_CASE(emptyStringTest); TEST_CASE(fflushOnInputStreamTest); + + TEST_CASE(sizeofsizeof); } void check(const char code[]) @@ -103,16 +105,19 @@ private: std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); - // Simplify token list.. - tokenizer.simplifyTokenList(); - - // Clear the error buffer.. - errout.str(""); - // Check.. Settings settings; settings._checkCodingStyle = true; CheckOther checkOther(&tokenizer, &settings, this); + + // Clear the error buffer.. + errout.str(""); + + checkOther.sizeofsizeof(); + + // Simplify token list.. + tokenizer.simplifyTokenList(); + checkOther.warningRedundantCode(); checkOther.checkZeroDivision(); checkOther.checkMathFunctions(); @@ -2503,6 +2508,15 @@ private: "}\n"); 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)