Checking variable scope

This commit is contained in:
Daniel Marjamäki 2008-03-17 11:05:30 +00:00
parent bcd292ffaa
commit 101afe4344
2 changed files with 21 additions and 2 deletions

View File

@ -579,7 +579,7 @@ static void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[
tok = tok->next; tok = tok->next;
// Check if the variable is used in this indentlevel.. // Check if the variable is used in this indentlevel..
bool used = false; bool used = false, used1 = false;
int indentlevel = 0; int indentlevel = 0;
while ( indentlevel >= 0 && tok ) while ( indentlevel >= 0 && tok )
{ {
@ -591,11 +591,16 @@ static void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[
else if ( tok->str[0] == '}' ) else if ( tok->str[0] == '}' )
{ {
indentlevel--; indentlevel--;
if ( indentlevel == 0 )
{
used1 = used;
used = false;
}
} }
else if ( strcmp(tok->str, varname) == 0 ) else if ( strcmp(tok->str, varname) == 0 )
{ {
if ( indentlevel == 0 ) if ( indentlevel == 0 || used1 )
return; return;
used = true; used = true;
} }

View File

@ -752,5 +752,19 @@ static void unused_variable()
"}\n"; "}\n";
check( CheckVariableScope, __LINE__, test4, "" ); check( CheckVariableScope, __LINE__, test4, "" );
const char test5[] = "static void f()\n"
"{\n"
" int i = 0;\n"
" {\n"
" i+5;\n"
" }\n"
" {\n"
" i+5;\n"
" }\n"
"}\n";
check( CheckVariableScope, __LINE__, test5, "" );
} }