'CheckUnsignedDivision' -> more errors. 'CheckVariableScope' -> removed false errors.
This commit is contained in:
parent
313d3dafa1
commit
c5e7cb4032
|
@ -505,6 +505,16 @@ void CheckUnsignedDivision()
|
|||
pattern_declvar[0] = ";";
|
||||
pattern_declvar[1] = "int";
|
||||
|
||||
pattern_declvar[2] = varname1;
|
||||
if ( findtoken(tokens, pattern_declvar) )
|
||||
var1_sign = 's';
|
||||
|
||||
pattern_declvar[2] = varname2;
|
||||
if ( findtoken(tokens, pattern_declvar) )
|
||||
var2_sign = 's';
|
||||
|
||||
pattern_declvar[0] = "{";
|
||||
|
||||
pattern_declvar[2] = varname1;
|
||||
if ( findtoken(tokens, pattern_declvar) )
|
||||
var1_sign = 's';
|
||||
|
@ -540,6 +550,42 @@ void CheckVariableScope()
|
|||
int indentlevel = 0;
|
||||
for ( TOKEN *tok = tokens; tok; tok = tok->next )
|
||||
{
|
||||
// Skip class and struct declarations..
|
||||
if ( strcmp(tok->str, "class") == 0 || strcmp(tok->str, "struct") == 0 )
|
||||
{
|
||||
for (TOKEN *tok2 = tok; tok2; tok2 = tok2->next)
|
||||
{
|
||||
if ( tok2->str[0] == '{' )
|
||||
{
|
||||
int _indentlevel = 0;
|
||||
tok = tok2;
|
||||
for (tok = tok2; tok; tok = tok->next)
|
||||
{
|
||||
if ( tok->str[0] == '{' )
|
||||
{
|
||||
_indentlevel++;
|
||||
}
|
||||
if ( tok->str[0] == '}' )
|
||||
{
|
||||
_indentlevel--;
|
||||
if ( _indentlevel <= 0 )
|
||||
{
|
||||
tok = tok->next;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (strchr(",);", tok2->str[0]))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( ! tok )
|
||||
break;
|
||||
}
|
||||
|
||||
if ( tok->str[0] == '{' )
|
||||
{
|
||||
indentlevel++;
|
||||
|
@ -587,6 +633,7 @@ static void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[
|
|||
// Check if the variable is used in this indentlevel..
|
||||
bool used = false, used1 = false;
|
||||
int indentlevel = 0;
|
||||
bool for_or_while = false;
|
||||
while ( indentlevel >= 0 && tok )
|
||||
{
|
||||
if ( tok->str[0] == '{' )
|
||||
|
@ -599,6 +646,8 @@ static void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[
|
|||
indentlevel--;
|
||||
if ( indentlevel == 0 )
|
||||
{
|
||||
if ( for_or_while && used )
|
||||
return;
|
||||
used1 = used;
|
||||
used = false;
|
||||
}
|
||||
|
@ -611,6 +660,14 @@ static void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[
|
|||
used = true;
|
||||
}
|
||||
|
||||
else if ( indentlevel==0 )
|
||||
{
|
||||
if ( strcmp(tok->str,"for")==0 || strcmp(tok->str,"while")==0 )
|
||||
for_or_while = true;
|
||||
if ( tok->str[0] == ';' )
|
||||
for_or_while = false;
|
||||
}
|
||||
|
||||
tok = tok->next;
|
||||
}
|
||||
|
||||
|
|
51
tests.cpp
51
tests.cpp
|
@ -689,12 +689,21 @@ static void division()
|
|||
|
||||
const char test1[] = "void f()\n"
|
||||
"{\n"
|
||||
" int ivar = -2;\n"
|
||||
" unsigned int uvar = 2;\n"
|
||||
" return -2 / uvar;\n"
|
||||
" return ivar / uvar;\n"
|
||||
"}\n";
|
||||
check( CheckUnsignedDivision, __LINE__, test1, "[test.cpp:4]: If the result is negative it will be wrong because an operand is unsigned.\n" );
|
||||
check( CheckUnsignedDivision, __LINE__, test1, "[test.cpp:5]: If the result is negative it will be wrong because an operand is unsigned.\n" );
|
||||
|
||||
|
||||
const char test2[] = "void f()\n"
|
||||
"{\n"
|
||||
" int ivar = -2;\n"
|
||||
" unsigned int uvar = 2;\n"
|
||||
" return uvar / ivar;\n"
|
||||
"}\n";
|
||||
check( CheckUnsignedDivision, __LINE__, test2, "[test.cpp:5]: If the result is negative it will be wrong because an operand is unsigned.\n" );
|
||||
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
|
@ -774,5 +783,43 @@ static void unused_variable()
|
|||
"}\n";
|
||||
check( CheckVariableScope, __LINE__, test6, "" );
|
||||
|
||||
|
||||
const char test7[] = "struct a\n"
|
||||
"{\n"
|
||||
" int x;\n"
|
||||
" int y;\n"
|
||||
"};\n";
|
||||
check( CheckVariableScope, __LINE__, test7, "" );
|
||||
|
||||
|
||||
const char test8[] = "static void f()\n"
|
||||
"{\n"
|
||||
" struct\n"
|
||||
" {\n"
|
||||
" int x;\n"
|
||||
" int y;\n"
|
||||
" } fred;\n"
|
||||
"}\n";
|
||||
check( CheckVariableScope, __LINE__, test8, "" );
|
||||
|
||||
|
||||
const char test9[] = "static void f()\n"
|
||||
"{\n"
|
||||
" int i;\n"
|
||||
" while (abc)\n"
|
||||
" {\n"
|
||||
" if (cond1)\n"
|
||||
" {\n"
|
||||
" i = 2;\n"
|
||||
" }\n"
|
||||
" if (cond2)\n"
|
||||
" {\n"
|
||||
" f(i);\n"
|
||||
" }\n"
|
||||
" }\n"
|
||||
"}\n";
|
||||
check( CheckVariableScope, __LINE__, test9, "" );
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue