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)
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)
{
@ -174,10 +174,7 @@ static void CheckBufferOverrun_LocalVariable()
break;
// Array index..
if (strcmp(tok2->str,varname)==0 &&
strcmp(getstr(tok2,1),"[")==0 &&
IsNumber(getstr(tok2,2)) &&
strcmp(getstr(tok2,3),"]")==0 )
if ( strcmp(tok2->str,varname)==0 && match( tok2->next, "[ num ]") )
{
const char *str = getstr(tok2, 2);
if (strtoul(str, NULL, 10) >= size)
@ -217,10 +214,11 @@ static void CheckBufferOverrun_LocalVariable()
// Loop..
const char *strindex = 0;
int value = 0;
if ( match(tok2, "for ( var = 0 ;") )
{
const char *strindex = 0;
int value = 0;
if (match(tok2,"for ( var = 0 ; var < num ; var + + )"))
{
strindex = getstr(tok2,2);
@ -241,30 +239,30 @@ static void CheckBufferOverrun_LocalVariable()
strindex = getstr(tok2,2);
value = 1 + atoi(getstr(tok2,8));
}
}
if (strindex && value>(int)size)
{
const TOKEN *tok3 = tok2;
while (tok3 && strcmp(tok3->str,")"))
tok3 = tok3->next;
if (!tok3)
break;
tok3 = tok3->next;
if (tok3->str[0] == '{')
tok3 = tok3->next;
while (tok3 && !strchr(";}",tok3->str[0]))
if (strindex && value>(int)size)
{
if (strcmp(tok3->str,varname)==0 &&
strcmp(getstr(tok3,1),"[")==0 &&
strcmp(getstr(tok3,2),strindex)==0 &&
strcmp(getstr(tok3,3),"]")==0 )
{
std::ostringstream ostr;
ostr << FileLine(tok3) << ": Buffer overrun";
ReportErr(ostr.str());
const TOKEN *tok3 = tok2;
while (tok3 && strcmp(tok3->str,")"))
tok3 = tok3->next;
if (!tok3)
break;
}
tok3 = tok3->next;
if (tok3->str[0] == '{')
tok3 = tok3->next;
while (tok3 && !strchr(";}",tok3->str[0]))
{
if ( match(tok3, "var [ var ]" &&
strcmp(tok3->str,varname)==0 &&
strcmp(getstr(tok3,2),strindex)==0 )
{
std::ostringstream ostr;
ostr << FileLine(tok3) << ": Buffer overrun";
ReportErr(ostr.str());
break;
}
tok3 = tok3->next;
}
}
}

View File

@ -16,7 +16,7 @@
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..
if (!match(tok, "( type * ) var"))
@ -194,15 +194,8 @@ void WarningIf()
}
// 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"))
continue;

View File

@ -69,3 +69,32 @@ bool setindentlevel( const TOKEN *tok, int &indentlevel, int 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 );
void GotoNextStatement( const TOKEN **tok );
void FindMatchingTokenInScope( const TOKEN **tok, const char pattern[], int &indentlevel );
//---------------------------------------------------------------------------