bug fixes for 'CheckVariableScope'

This commit is contained in:
Daniel Marjamäki 2008-03-16 17:39:38 +00:00
parent a9524b9207
commit 119e719d52
3 changed files with 40 additions and 14 deletions

View File

@ -550,13 +550,20 @@ void CheckVariableScope()
} }
if ( indentlevel > 0 && func && strchr("{};", tok->str[0]) ) if ( indentlevel > 0 && func && strchr("{};", tok->str[0]) )
{ {
// Variable declaration? // First token of statement..
if (match(tok->next, "var var ;")) TOKEN *tok1 = tok->next;
CheckVariableScope_LookupVar( tok->next, getstr(tok, 2) );
if (strcmp(tok1->str,"return")==0 ||
strcmp(tok1->str,"delete")==0 ||
strcmp(tok1->str,"else")==0)
continue;
// Variable declaration? // Variable declaration?
else if (match(tok->next, "var var =")) if (match(tok1, "var var ;") ||
CheckVariableScope_LookupVar( tok->next, getstr(tok, 2) ); match(tok1, "var var =") )
{
CheckVariableScope_LookupVar( tok1, getstr(tok1, 1) );
}
} }
} }
@ -568,7 +575,6 @@ static void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[
const TOKEN *tok = tok1; const TOKEN *tok = tok1;
// Skip the variable declaration.. // Skip the variable declaration..
tok = tok->next;
while ( tok->str[0] != ';' ) while ( tok->str[0] != ';' )
tok = tok->next; tok = tok->next;

View File

@ -219,6 +219,9 @@ static void CppCheck(const char FileName[])
// Dangerous usage of strtok // Dangerous usage of strtok
// Disabled because it generates false positives // Disabled because it generates false positives
//WarningStrTok(); //WarningStrTok();
// Variable scope
CheckVariableScope();
} }

View File

@ -724,16 +724,33 @@ static void unused_variable()
" }\n" " }\n"
"}\n"; "}\n";
check( CheckVariableScope, __LINE__, test2, "[test.cpp:3] The scope of the variable 'i' can be limited\n" ); check( CheckVariableScope, __LINE__, test2, "[test.cpp:3] The scope of the variable 'i' can be limited\n" );
/*
const char test3[] = "void f()\n"
const char test3[] = "static void DeleteNextToken(TOKEN *tok)\n"
"{\n" "{\n"
" int i = 0;\n" " TOKEN *next = tok->next;\n"
" while (abc)\n" " tok->next = next->next;\n"
" {\n" " free(next->str);\n"
" i = i + 1;\n" " delete next;\n"
" }\n"
"}\n"; "}\n";
check( CheckVariableScope, __LINE__, test3, "" ); check( CheckVariableScope, __LINE__, test3, "" );
*/
const char test4[] = "static void f()\n"
"{\n"
" bool special = false;\n"
" do\n"
" {\n"
" // Special sequence\n"
" if (special)\n"
" special = false;\n"
" else\n"
" special = (c == \'\\\');\n"
" }\n"
" while (special || c != \'\"\');\n"
"}\n";
check( CheckVariableScope, __LINE__, test4, "" );
} }