Reverted 205:207

This commit is contained in:
Daniel Marjamäki 2008-03-23 13:38:01 +00:00
parent 34a2fdfb37
commit 75debeba37
6 changed files with 249 additions and 240 deletions

View File

@ -16,9 +16,11 @@ static const TOKEN *findfunction(const TOKEN *tok)
int indentlevel = 0, parlevel = 0;
for (; tok; tok = tok->next)
{
setindentlevel( tok, indentlevel, -1 );
if (tok->str[0] == '(')
if (tok->str[0] == '{')
indentlevel++;
else if (tok->str[0] == '}')
indentlevel--;
else if (tok->str[0] == '(')
parlevel++;
else if (tok->str[0] == ')')
parlevel--;
@ -26,7 +28,7 @@ static const TOKEN *findfunction(const TOKEN *tok)
if (!tok->next)
break;
if (indentlevel==0 && parlevel==0 && match(tok,"var ("))
if (indentlevel==0 && parlevel==0 && IsName(tok->str) && tok->next->str[0]=='(')
{
for (const TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next)
{
@ -111,8 +113,12 @@ static void CheckBufferOverrun_DynamicData()
int indentlevel = 0;
for (const TOKEN *tok = ftok; tok; tok = tok->next)
{
if (setindentlevel(tok, indentlevel, 0))
if (tok->str[0] == '{')
indentlevel++;
else if (tok->str[0] == '}')
{
indentlevel--;
if (indentlevel <= 0)
break;
}
@ -154,27 +160,42 @@ static void CheckBufferOverrun_LocalVariable()
int indentlevel = 0;
for (const TOKEN *tok = tokens; tok; tok = tok->next)
{
if (setindentlevel( tok, indentlevel, -1 ))
break;
if (tok->str[0]=='{')
indentlevel++;
else if (tok->str[0]=='}')
indentlevel--;
else if (indentlevel > 0)
{
// Declaring array..
if ( ! match(tok, "type var [ num ] ;") )
continue;
if (match(tok, "type var [ num ] ;"))
{
const char *varname = getstr(tok,1);
unsigned int size = strtoul(getstr(tok,3), NULL, 10);
int total_size = size * SizeOfType(tok->str);
if (total_size == 0)
continue;
int _indentlevel = 0;
int _indentlevel = indentlevel;
for (const TOKEN *tok2 = gettok(tok,5); tok2; tok2 = tok2->next)
{
if ( setindentlevel(tok2, _indentlevel, -1) )
if (tok2->str[0]=='{')
{
_indentlevel++;
}
else if (tok2->str[0]=='}')
{
_indentlevel--;
if (_indentlevel < indentlevel)
break;
}
else
{
// Array index..
if ( strcmp(tok2->str,varname)==0 && match( tok2->next, "[ num ]") )
if (strcmp(tok2->str,varname)==0 &&
strcmp(getstr(tok2,1),"[")==0 &&
IsNumber(getstr(tok2,2)) &&
strcmp(getstr(tok2,3),"]")==0 )
{
const char *str = getstr(tok2, 2);
if (strtoul(str, NULL, 10) >= size)
@ -214,11 +235,10 @@ static void CheckBufferOverrun_LocalVariable()
// Loop..
if ( match(tok2, "for ( var = 0 ;") )
{
const char *strindex = 0;
int value = 0;
if ( match(tok2, "for ( var = 0 ;") )
{
if (match(tok2,"for ( var = 0 ; var < num ; var + + )"))
{
strindex = getstr(tok2,2);
@ -239,7 +259,7 @@ static void CheckBufferOverrun_LocalVariable()
strindex = getstr(tok2,2);
value = 1 + atoi(getstr(tok2,8));
}
}
if (strindex && value>(int)size)
{
const TOKEN *tok3 = tok2;
@ -252,9 +272,10 @@ static void CheckBufferOverrun_LocalVariable()
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 )
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";
@ -264,7 +285,6 @@ static void CheckBufferOverrun_LocalVariable()
tok3 = tok3->next;
}
}
}
// Writing data into array..
@ -295,6 +315,9 @@ static void CheckBufferOverrun_LocalVariable()
}
}
}
}
}
}
//---------------------------------------------------------------------------
static void CheckBufferOverrun_StructVariable_CheckVar( const TOKEN *tok1, const char varname[], const char dot[], const char arrname[], const int arrsize )

View File

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

View File

@ -16,7 +16,7 @@
void WarningOldStylePointerCast()
{
for (const TOKEN *tok = FindMatchingToken(tokens); tok; tok = tok->next)
for (const TOKEN *tok = tokens; tok; tok = tok->next)
{
// Old style pointer casting..
if (!match(tok, "( type * ) var"))
@ -194,8 +194,15 @@ void WarningIf()
}
// Search for 'a=b; if (a==b)'
for (const TOKEN *tok = tokens; tok; GotoNextStatement(&tok))
for (const TOKEN *tok = tokens; tok; tok = tok->next)
{
// Begin statement?
if ( ! strchr(";{}", tok->str[0]) )
continue;
tok = tok->next;
if ( ! tok )
break;
if (!match(tok,"var = var ; if ( var"))
continue;
@ -298,10 +305,17 @@ static const TOKEN *GetFunction( const TOKEN *content )
int indentlevel = 0;
for (const TOKEN *tok = tokens; tok; tok = tok->next)
{
if (setindentlevel(tok, indentlevel, 0))
func = NULL;
if ( tok->str[0] == '{' )
indentlevel++;
else if ( tok->str[0] == '}' )
{
indentlevel--;
if (indentlevel == 0)
func = NULL;
}
else if (indentlevel == 0)
{
if (tok->str[0] == ';')
func = NULL;
@ -352,14 +366,20 @@ void WarningStrTok()
int indentlevel = 0;
for ( const TOKEN *tok = *it1; tok; tok = tok->next )
{
if (setindentlevel(tok, indentlevel, 0))
if ( tok->str[0] == '{' )
indentlevel++;
else if ( tok->str[0] == '}' )
{
if ( indentlevel <= 1 )
break;
indentlevel--;
}
if ( indentlevel == 0 )
continue;
else if ( indentlevel >= 1 )
{
// Only interested in function calls..
if (! match(tok, "var ("))
if (!(IsName(tok->str) && strcmp(getstr(tok,1), "(") == 0))
continue;
// Check if function name is in funclist..
@ -377,6 +397,7 @@ void WarningStrTok()
}
}
}
}
//---------------------------------------------------------------------------
@ -415,13 +436,18 @@ void CheckCaseWithoutBreak()
int indentlevel = 0;
for (const TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next)
{
if ( setindentlevel( tok2, indentlevel, -1 ) )
if (tok2->str[0] == '{')
indentlevel++;
else if (tok2->str[0] == '}')
{
indentlevel--;
if (indentlevel < 0)
{
std::ostringstream ostr;
ostr << FileLine(tok) << ": 'case' without 'break'.";
ReportErr(ostr.str());
}
}
if (indentlevel==0)
{
if (strcmp(tok2->str,"break")==0)
@ -438,6 +464,7 @@ void CheckCaseWithoutBreak()
}
}
}
}
//---------------------------------------------------------------------------
@ -534,12 +561,20 @@ void CheckVariableScope()
tok = tok2;
for (tok = tok2; tok; tok = tok->next)
{
if ( setindentlevel( tok, _indentlevel, 0 ) )
if ( tok->str[0] == '{' )
{
_indentlevel++;
}
if ( tok->str[0] == '}' )
{
_indentlevel--;
if ( _indentlevel <= 0 )
{
tok = tok->next;
break;
}
}
}
break;
}
if (strchr(",);", tok2->str[0]))
@ -551,11 +586,16 @@ void CheckVariableScope()
break;
}
if ( setindentlevel( tok, indentlevel, 0 ) )
if ( tok->str[0] == '{' )
{
indentlevel++;
}
if ( tok->str[0] == '}' )
{
indentlevel--;
if ( indentlevel == 0 )
func = false;
}
if ( indentlevel == 0 && match(tok, ") {") )
{
func = true;
@ -592,18 +632,25 @@ static void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[
// Check if the variable is used in this indentlevel..
bool used = false, used1 = false;
bool for_or_while = false;
int indentlevel = 0;
for (; tok; tok = tok->next )
bool for_or_while = false;
while ( indentlevel >= 0 && tok )
{
if ( setindentlevel( tok, indentlevel, 0 ) )
if ( tok->str[0] == '{' )
{
indentlevel++;
}
else if ( tok->str[0] == '}' )
{
indentlevel--;
if ( indentlevel == 0 )
{
if ( for_or_while && used )
return;
used1 = used;
used = false;
if ( indentlevel < 0 )
break;
}
}
else if ( strcmp(tok->str, varname) == 0 )
@ -620,6 +667,8 @@ static void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[
if ( tok->str[0] == ';' )
for_or_while = false;
}
tok = tok->next;
}
// Warning if "used" is true

View File

@ -58,43 +58,3 @@ bool IsStandardType(const char str[])
}
//---------------------------------------------------------------------------
bool setindentlevel( const TOKEN *tok, int &indentlevel, int endlevel )
{
if ( tok->str[0] == '{' )
indentlevel++;
else if ( tok->str[0] == '}' )
indentlevel--;
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

@ -6,10 +6,6 @@
#include <string>
#include <sstream>
//---------------------------------------------------------------------------
// Report errors..
//---------------------------------------------------------------------------
struct TOKEN;
std::string FileLine(const TOKEN *tok);
@ -20,24 +16,10 @@ void ReportErr(const std::string &errmsg);
extern std::ostringstream errout;
//---------------------------------------------------------------------------
// Classify tokens..
//---------------------------------------------------------------------------
bool IsName(const char str[]);
bool IsNumber(const char str[]);
bool IsStandardType(const char str[]);
//---------------------------------------------------------------------------
// Iterating through tokens..
//---------------------------------------------------------------------------
bool setindentlevel( const TOKEN *tok, int &indentlevel, int endlevel );
void GotoNextStatement( const TOKEN **tok );
void FindMatchingTokenInScope( const TOKEN **tok, const char pattern[], int &indentlevel );
//---------------------------------------------------------------------------
#endif

View File

@ -494,16 +494,6 @@ static void constructors()
"}\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" );
}
//---------------------------------------------------------------------------