From c77b81fddd5370f664557dde9fba59a48d90120d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 16 Sep 2008 05:57:57 +0000 Subject: [PATCH] CheckOther: Minor updates in checking of signed char variables and unsigned division --- CheckOther.cpp | 6 +++--- testcharvar.cpp | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CheckOther.cpp b/CheckOther.cpp index dfb82e418..cf715a386 100644 --- a/CheckOther.cpp +++ b/CheckOther.cpp @@ -330,7 +330,7 @@ void CheckUnsignedDivision() else if ( Match(tok, "[{};(,] unsigned %type% %var% [;=,)]") ) varsign[getstr(tok,3)] = 'u'; - else if ( tok->str[0] != ')' && Match(tok->next, "%var% / %var%") ) + else if (!Match(tok,"[).]") && Match(tok->next, "%var% / %var%")) { const char *varname1 = getstr(tok,1); const char *varname2 = getstr(tok,3); @@ -618,7 +618,7 @@ void CheckCharVariable() for (const TOKEN *tok = tokens; tok; tok = tok->next) { // Declaring the variable.. - if ( Match(tok, "[{};] char %var% [;=,]") ) + if ( Match(tok, "[{};(,] char %var% [;=,)]") ) { const char *varname[2] = {0}; varname[0] = getstr(tok, 2); @@ -645,7 +645,7 @@ void CheckCharVariable() break; } - else if ( Match(tok2, "[&|] %var1%") ) + else if ( Match(tok2, "[&|] %var1%", varname) || Match(tok2, "%var1% [&|]", varname) ) { std::ostringstream errmsg; errmsg << FileLine(tok2) << ": Warning - using char variable in bit operation"; diff --git a/testcharvar.cpp b/testcharvar.cpp index b7962bfea..7e4a3511c 100644 --- a/testcharvar.cpp +++ b/testcharvar.cpp @@ -36,6 +36,7 @@ public: TEST_FIXTURE( TestCharVar ) { TEST_CASE( array_index ); + TEST_CASE( bitop ); } @@ -54,8 +55,24 @@ public: " buf[ch] = 0;\n" "}\n" ); ASSERT_EQUALS( std::string("[test.cpp:4]: Warning - using char variable as array index\n"), errout.str() ); + + check( "void foo(char ch)\n" + "{\n" + " buf[ch] = 0;\n" + "}\n" ); + ASSERT_EQUALS( std::string("[test.cpp:3]: Warning - using char variable as array index\n"), errout.str() ); } + + void bitop() + { + check( "void foo()\n" + "{\n" + " char ch;\n" + " result = a | ch;\n" + "}\n" ); + ASSERT_EQUALS( std::string("[test.cpp:4]: Warning - using char variable in bit operation\n"), errout.str() ); + } }; REGISTER_FIXTURE( TestCharVar )