CheckUnsignedDivision: Made it more accurate and moved it to the
standard checks
This commit is contained in:
parent
c9fc2594e8
commit
5115420809
|
@ -325,7 +325,11 @@ void CheckUnsignedDivision()
|
|||
for ( TOKEN *tok = tokens; tok; tok = tok->next )
|
||||
{
|
||||
if ( Match(tok, "[{};(,] %type% %var% [;=,)]") )
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
5
main.cpp
5
main.cpp
|
@ -260,9 +260,8 @@ 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 )
|
||||
// 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
|
||||
|
|
|
@ -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 )
|
||||
|
|
Loading…
Reference in New Issue