CheckUnsignedDivision: Made it more accurate and moved it to the

standard checks
This commit is contained in:
Daniel Marjamäki 2008-10-04 12:12:24 +00:00
parent c9fc2594e8
commit 5115420809
3 changed files with 21 additions and 6 deletions

View File

@ -325,7 +325,11 @@ void CheckUnsignedDivision()
for ( TOKEN *tok = tokens; tok; tok = tok->next )
{
if ( Match(tok, "[{};(,] %type% %var% [;=,)]") )
varsign[getstr(tok,2)] = 's';
{
const char *type = getstr(tok, 1);
if (strcmp(type,"char")==0 || strcmp(type,"short")==0 || strcmp(type,"int")==0)
varsign[getstr(tok,2)] = 's';
}
else if ( Match(tok, "[{};(,] unsigned %type% %var% [;=,)]") )
varsign[getstr(tok,3)] = 'u';
@ -634,7 +638,7 @@ void CheckCharVariable()
else if ( tok2->str[0] == '}' )
{
--indentlevel;
if ( indentlevel < 0 )
if ( indentlevel <= 0 )
break;
}

View File

@ -260,10 +260,9 @@ static void CppCheck(const char FileName[], unsigned int FileId)
CheckMemset();
// Check for unwanted unsigned division
// Not accurate yet. Very important to run it before 'SimplifyTokenList'
if ( ShowAll )
CheckUnsignedDivision();
// Check for unsigned divisions where one operand is signed
// Very important to run it before 'SimplifyTokenList'
CheckUnsignedDivision();
// Give warning when using char variable as array index
// Doesn't work on simplified token list ('unsigned')

View File

@ -38,6 +38,7 @@ public:
TEST_CASE( division2 );
TEST_CASE( division3 );
TEST_CASE( division4 );
TEST_CASE( division5 );
}
void division1()
@ -89,6 +90,17 @@ public:
);
ASSERT_EQUALS( std::string(""), errout.str() );
}
void division5()
{
check( "#define USER_HASH (16)\n"
"void foo()\n"
"{\n"
" unsigned int val = 32;\n"
" val = val / USER_HASH;\n"
);
ASSERT_EQUALS( std::string(""), errout.str() );
}
};
REGISTER_FIXTURE( TestDivision )