Refactoring: Use 'setindentlevel'

This commit is contained in:
Daniel Marjamäki 2008-03-23 07:57:27 +00:00
parent 0d0f562e90
commit 3dfc79d3b6
6 changed files with 58 additions and 98 deletions

View File

@ -16,7 +16,7 @@ static const TOKEN *findfunction(const TOKEN *tok)
int indentlevel = 0, parlevel = 0; int indentlevel = 0, parlevel = 0;
for (; tok; tok = tok->next) for (; tok; tok = tok->next)
{ {
setindentlevel( tok, indentlevel ); setindentlevel( tok, indentlevel, -1 );
if (tok->str[0] == '(') if (tok->str[0] == '(')
parlevel++; parlevel++;
@ -111,9 +111,8 @@ static void CheckBufferOverrun_DynamicData()
int indentlevel = 0; int indentlevel = 0;
for (const TOKEN *tok = ftok; tok; tok = tok->next) for (const TOKEN *tok = ftok; tok; tok = tok->next)
{ {
if (setindentlevel(tok, indentlevel)) if (setindentlevel(tok, indentlevel, 0))
{ {
if (indentlevel <= 0)
break; break;
} }
@ -155,8 +154,7 @@ static void CheckBufferOverrun_LocalVariable()
int indentlevel = 0; int indentlevel = 0;
for (const TOKEN *tok = tokens; tok; tok = tok->next) for (const TOKEN *tok = tokens; tok; tok = tok->next)
{ {
setindentlevel( tok, indentlevel ); if (setindentlevel( tok, indentlevel, -1 ))
if (indentlevel < 0)
break; break;
// Declaring array.. // Declaring array..
@ -172,8 +170,7 @@ static void CheckBufferOverrun_LocalVariable()
int _indentlevel = 0; int _indentlevel = 0;
for (const TOKEN *tok2 = gettok(tok,5); tok2; tok2 = tok2->next) for (const TOKEN *tok2 = gettok(tok,5); tok2; tok2 = tok2->next)
{ {
setindentlevel(tok2, _indentlevel); if ( setindentlevel(tok2, _indentlevel, -1) )
if ( _indentlevel < 0 )
break; break;
// Array index.. // Array index..

View File

@ -91,12 +91,7 @@ void WarningIncludeHeader()
continue; continue;
// I'm only interested in stuff that is declared at indentlevel 0 // I'm only interested in stuff that is declared at indentlevel 0
if (tok1->str[0] == '{') setindentlevel( tok1, indentlevel, -1 );
indentlevel++;
else if (tok1->str[0] == '}')
indentlevel--;
if (indentlevel != 0) if (indentlevel != 0)
continue; continue;

View File

@ -305,17 +305,10 @@ static const TOKEN *GetFunction( const TOKEN *content )
int indentlevel = 0; int indentlevel = 0;
for (const TOKEN *tok = tokens; tok; tok = tok->next) for (const TOKEN *tok = tokens; tok; tok = tok->next)
{ {
if ( tok->str[0] == '{' ) if (setindentlevel(tok, indentlevel, 0))
indentlevel++;
else if ( tok->str[0] == '}' )
{
indentlevel--;
if (indentlevel == 0)
func = NULL; func = NULL;
}
else if (indentlevel == 0) if (indentlevel == 0)
{ {
if (tok->str[0] == ';') if (tok->str[0] == ';')
func = NULL; func = NULL;
@ -366,20 +359,14 @@ void WarningStrTok()
int indentlevel = 0; int indentlevel = 0;
for ( const TOKEN *tok = *it1; tok; tok = tok->next ) for ( const TOKEN *tok = *it1; tok; tok = tok->next )
{ {
if ( tok->str[0] == '{' ) if (setindentlevel(tok, indentlevel, 0))
indentlevel++;
else if ( tok->str[0] == '}' )
{
if ( indentlevel <= 1 )
break; break;
indentlevel--;
}
else if ( indentlevel >= 1 ) if ( indentlevel == 0 )
{ continue;
// Only interested in function calls.. // Only interested in function calls..
if (!(IsName(tok->str) && strcmp(getstr(tok,1), "(") == 0)) if (! match(tok, "var ("))
continue; continue;
// Check if function name is in funclist.. // Check if function name is in funclist..
@ -397,7 +384,6 @@ void WarningStrTok()
} }
} }
} }
}
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -436,18 +422,13 @@ void CheckCaseWithoutBreak()
int indentlevel = 0; int indentlevel = 0;
for (const TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next) for (const TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next)
{ {
if (tok2->str[0] == '{') if ( setindentlevel( tok2, indentlevel, -1 ) )
indentlevel++;
else if (tok2->str[0] == '}')
{
indentlevel--;
if (indentlevel < 0)
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << FileLine(tok) << ": 'case' without 'break'."; ostr << FileLine(tok) << ": 'case' without 'break'.";
ReportErr(ostr.str()); ReportErr(ostr.str());
} }
}
if (indentlevel==0) if (indentlevel==0)
{ {
if (strcmp(tok2->str,"break")==0) if (strcmp(tok2->str,"break")==0)
@ -464,7 +445,6 @@ void CheckCaseWithoutBreak()
} }
} }
} }
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -561,20 +541,12 @@ void CheckVariableScope()
tok = tok2; tok = tok2;
for (tok = tok2; tok; tok = tok->next) for (tok = tok2; tok; tok = tok->next)
{ {
if ( tok->str[0] == '{' ) if ( setindentlevel( tok, _indentlevel, 0 ) )
{
_indentlevel++;
}
if ( tok->str[0] == '}' )
{
_indentlevel--;
if ( _indentlevel <= 0 )
{ {
tok = tok->next; tok = tok->next;
break; break;
} }
} }
}
break; break;
} }
if (strchr(",);", tok2->str[0])) if (strchr(",);", tok2->str[0]))
@ -586,16 +558,11 @@ void CheckVariableScope()
break; break;
} }
if ( tok->str[0] == '{' ) if ( setindentlevel( tok, indentlevel, 0 ) )
{ {
indentlevel++;
}
if ( tok->str[0] == '}' )
{
indentlevel--;
if ( indentlevel == 0 )
func = false; func = false;
} }
if ( indentlevel == 0 && match(tok, ") {") ) if ( indentlevel == 0 && match(tok, ") {") )
{ {
func = true; func = true;
@ -632,25 +599,18 @@ static void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[
// Check if the variable is used in this indentlevel.. // Check if the variable is used in this indentlevel..
bool used = false, used1 = false; bool used = false, used1 = false;
int indentlevel = 0;
bool for_or_while = false; bool for_or_while = false;
while ( indentlevel >= 0 && tok ) int indentlevel = 0;
for (; tok; tok = tok->next )
{ {
if ( tok->str[0] == '{' ) if ( setindentlevel( tok, indentlevel, 0 ) )
{
indentlevel++;
}
else if ( tok->str[0] == '}' )
{
indentlevel--;
if ( indentlevel == 0 )
{ {
if ( for_or_while && used ) if ( for_or_while && used )
return; return;
used1 = used; used1 = used;
used = false; used = false;
} if ( indentlevel < 0 )
break;
} }
else if ( strcmp(tok->str, varname) == 0 ) else if ( strcmp(tok->str, varname) == 0 )
@ -667,8 +627,6 @@ static void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[
if ( tok->str[0] == ';' ) if ( tok->str[0] == ';' )
for_or_while = false; for_or_while = false;
} }
tok = tok->next;
} }
// Warning if "used" is true // Warning if "used" is true

View File

@ -58,7 +58,7 @@ bool IsStandardType(const char str[])
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool setindentlevel( const TOKEN *tok, int &indentlevel ) bool setindentlevel( const TOKEN *tok, int &indentlevel, int endlevel )
{ {
if ( tok->str[0] == '{' ) if ( tok->str[0] == '{' )
indentlevel++; indentlevel++;
@ -66,6 +66,6 @@ bool setindentlevel( const TOKEN *tok, int &indentlevel )
else if ( tok->str[0] == '}' ) else if ( tok->str[0] == '}' )
indentlevel--; indentlevel--;
return bool(tok->str[0] == '}'); return bool(tok->str[0]=='}' && indentlevel<=endlevel);
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -34,7 +34,7 @@ bool IsStandardType(const char str[]);
// Iterating through tokens.. // Iterating through tokens..
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool setindentlevel( const TOKEN *tok, int &indentlevel ); bool setindentlevel( const TOKEN *tok, int &indentlevel, int endlevel );
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -494,6 +494,16 @@ static void constructors()
"}\n"; "}\n";
check( CheckConstructors, __LINE__, test4, "[test.cpp:8] Uninitialized member variable 'Fred::i'\n" ); check( CheckConstructors, __LINE__, test4, "[test.cpp:8] Uninitialized member variable 'Fred::i'\n" );
const char test5[] = "class Fred\n"
"{\n"
"public:\n"
" unsigned int i;\n"
"};\n";
check( CheckConstructors, __LINE__, test5, "[test.cpp:1] The class 'Fred' has no constructor\n" );
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------