Refactoring

This commit is contained in:
Daniel Marjamäki 2008-03-23 13:27:34 +00:00
parent 3dfc79d3b6
commit 34a2fdfb37
4 changed files with 59 additions and 37 deletions

View File

@ -26,7 +26,7 @@ static const TOKEN *findfunction(const TOKEN *tok)
if (!tok->next) if (!tok->next)
break; break;
if (indentlevel==0 && parlevel==0 && IsName(tok->str) && tok->next->str[0]=='(') if (indentlevel==0 && parlevel==0 && match(tok,"var ("))
{ {
for (const TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next) for (const TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next)
{ {
@ -174,10 +174,7 @@ static void CheckBufferOverrun_LocalVariable()
break; break;
// Array index.. // Array index..
if (strcmp(tok2->str,varname)==0 && if ( strcmp(tok2->str,varname)==0 && match( tok2->next, "[ num ]") )
strcmp(getstr(tok2,1),"[")==0 &&
IsNumber(getstr(tok2,2)) &&
strcmp(getstr(tok2,3),"]")==0 )
{ {
const char *str = getstr(tok2, 2); const char *str = getstr(tok2, 2);
if (strtoul(str, NULL, 10) >= size) if (strtoul(str, NULL, 10) >= size)
@ -217,10 +214,11 @@ static void CheckBufferOverrun_LocalVariable()
// Loop.. // Loop..
const char *strindex = 0;
int value = 0;
if ( match(tok2, "for ( var = 0 ;") ) if ( match(tok2, "for ( var = 0 ;") )
{ {
const char *strindex = 0;
int value = 0;
if (match(tok2,"for ( var = 0 ; var < num ; var + + )")) if (match(tok2,"for ( var = 0 ; var < num ; var + + )"))
{ {
strindex = getstr(tok2,2); strindex = getstr(tok2,2);
@ -241,7 +239,7 @@ static void CheckBufferOverrun_LocalVariable()
strindex = getstr(tok2,2); strindex = getstr(tok2,2);
value = 1 + atoi(getstr(tok2,8)); value = 1 + atoi(getstr(tok2,8));
} }
}
if (strindex && value>(int)size) if (strindex && value>(int)size)
{ {
const TOKEN *tok3 = tok2; const TOKEN *tok3 = tok2;
@ -254,10 +252,9 @@ static void CheckBufferOverrun_LocalVariable()
tok3 = tok3->next; tok3 = tok3->next;
while (tok3 && !strchr(";}",tok3->str[0])) while (tok3 && !strchr(";}",tok3->str[0]))
{ {
if (strcmp(tok3->str,varname)==0 && if ( match(tok3, "var [ var ]" &&
strcmp(getstr(tok3,1),"[")==0 && strcmp(tok3->str,varname)==0 &&
strcmp(getstr(tok3,2),strindex)==0 && strcmp(getstr(tok3,2),strindex)==0 )
strcmp(getstr(tok3,3),"]")==0 )
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(tok3) << ": Buffer overrun"; ostr << FileLine(tok3) << ": Buffer overrun";
@ -267,6 +264,7 @@ static void CheckBufferOverrun_LocalVariable()
tok3 = tok3->next; tok3 = tok3->next;
} }
} }
}
// Writing data into array.. // Writing data into array..

View File

@ -16,7 +16,7 @@
void WarningOldStylePointerCast() void WarningOldStylePointerCast()
{ {
for (const TOKEN *tok = tokens; tok; tok = tok->next) for (const TOKEN *tok = FindMatchingToken(tokens); tok; tok = tok->next)
{ {
// Old style pointer casting.. // Old style pointer casting..
if (!match(tok, "( type * ) var")) if (!match(tok, "( type * ) var"))
@ -194,15 +194,8 @@ void WarningIf()
} }
// Search for 'a=b; if (a==b)' // Search for 'a=b; if (a==b)'
for (const TOKEN *tok = tokens; tok; tok = tok->next) for (const TOKEN *tok = tokens; tok; GotoNextStatement(&tok))
{ {
// Begin statement?
if ( ! strchr(";{}", tok->str[0]) )
continue;
tok = tok->next;
if ( ! tok )
break;
if (!match(tok,"var = var ; if ( var")) if (!match(tok,"var = var ; if ( var"))
continue; continue;

View File

@ -69,3 +69,32 @@ bool setindentlevel( const TOKEN *tok, int &indentlevel, int endlevel )
return bool(tok->str[0]=='}' && indentlevel<=endlevel); return bool(tok->str[0]=='}' && indentlevel<=endlevel);
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void GotoNextStatement( const TOKEN **tok )
{
// Goto end of statement..
while ( *tok && ! strchr("{};", (*tok)->str[0]) )
*tok = (*tok)->next;
// Goto next statement
if ( *tok )
*tok = (*tok)->next;
}
//---------------------------------------------------------------------------
void FindMatchingTokenInScope( const TOKEN **tok, const char pattern[], int &indentlevel )
{
while ( *tok )
{
if ( setindentlevel( *tok, indentlevel, -1 ) )
*tok = NULL;
else if ( match( *tok, pattern ) )
return;
else
*tok = (*tok)->next;
}
}
//---------------------------------------------------------------------------

View File

@ -35,6 +35,8 @@ bool IsStandardType(const char str[]);
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool setindentlevel( const TOKEN *tok, int &indentlevel, int endlevel ); bool setindentlevel( const TOKEN *tok, int &indentlevel, int endlevel );
void GotoNextStatement( const TOKEN **tok );
void FindMatchingTokenInScope( const TOKEN **tok, const char pattern[], int &indentlevel );
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------