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 )
|
for ( TOKEN *tok = tokens; tok; tok = tok->next )
|
||||||
{
|
{
|
||||||
if ( Match(tok, "[{};(,] %type% %var% [;=,)]") )
|
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% [;=,)]") )
|
else if ( Match(tok, "[{};(,] unsigned %type% %var% [;=,)]") )
|
||||||
varsign[getstr(tok,3)] = 'u';
|
varsign[getstr(tok,3)] = 'u';
|
||||||
|
@ -634,7 +638,7 @@ void CheckCharVariable()
|
||||||
else if ( tok2->str[0] == '}' )
|
else if ( tok2->str[0] == '}' )
|
||||||
{
|
{
|
||||||
--indentlevel;
|
--indentlevel;
|
||||||
if ( indentlevel < 0 )
|
if ( indentlevel <= 0 )
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
7
main.cpp
7
main.cpp
|
@ -260,10 +260,9 @@ static void CppCheck(const char FileName[], unsigned int FileId)
|
||||||
CheckMemset();
|
CheckMemset();
|
||||||
|
|
||||||
|
|
||||||
// Check for unwanted unsigned division
|
// Check for unsigned divisions where one operand is signed
|
||||||
// Not accurate yet. Very important to run it before 'SimplifyTokenList'
|
// Very important to run it before 'SimplifyTokenList'
|
||||||
if ( ShowAll )
|
CheckUnsignedDivision();
|
||||||
CheckUnsignedDivision();
|
|
||||||
|
|
||||||
// Give warning when using char variable as array index
|
// Give warning when using char variable as array index
|
||||||
// Doesn't work on simplified token list ('unsigned')
|
// Doesn't work on simplified token list ('unsigned')
|
||||||
|
|
|
@ -38,6 +38,7 @@ public:
|
||||||
TEST_CASE( division2 );
|
TEST_CASE( division2 );
|
||||||
TEST_CASE( division3 );
|
TEST_CASE( division3 );
|
||||||
TEST_CASE( division4 );
|
TEST_CASE( division4 );
|
||||||
|
TEST_CASE( division5 );
|
||||||
}
|
}
|
||||||
|
|
||||||
void division1()
|
void division1()
|
||||||
|
@ -89,6 +90,17 @@ public:
|
||||||
);
|
);
|
||||||
ASSERT_EQUALS( std::string(""), errout.str() );
|
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 )
|
REGISTER_FIXTURE( TestDivision )
|
||||||
|
|
Loading…
Reference in New Issue