In the checks, use const pointers

This commit is contained in:
Daniel Marjamäki 2008-03-22 11:46:06 +00:00
parent 13f681879f
commit ab7cb379af
5 changed files with 87 additions and 75 deletions

View File

@ -50,12 +50,12 @@ TOKEN *findfunction(TOKEN *tok)
extern bool ShowAll;
static void _DynamicDataCheck(TOKEN *ftok, TOKEN *tok)
static void _DynamicDataCheck(const TOKEN *ftok, const TOKEN *tok)
{
const char *var2 = tok->str;
bool decl = false;
unsigned int Var2Count = 0;
for ( TOKEN *tok2 = ftok; tok2; tok2 = tok2->next )
for ( const TOKEN *tok2 = ftok; tok2; tok2 = tok2->next )
{
if (tok2 == tok)
break;
@ -109,10 +109,10 @@ static void _DynamicDataCheck(TOKEN *ftok, TOKEN *tok)
static void _DynamicData()
{
for (TOKEN *ftok = findfunction(tokens); ftok; ftok = findfunction(ftok->next))
for (const TOKEN *ftok = findfunction(tokens); ftok; ftok = findfunction(ftok->next))
{
int indentlevel = 0;
for (TOKEN *tok = ftok; tok; tok = tok->next)
for (const TOKEN *tok = ftok; tok; tok = tok->next)
{
if (tok->str[0] == '{')
indentlevel++;
@ -132,7 +132,7 @@ static void _DynamicData()
if (match(tok,"sprintf ( var"))
{
for ( TOKEN *tok2 = gettok(tok,3); tok2; tok2 = tok2->next )
for ( const TOKEN *tok2 = gettok(tok,3); tok2; tok2 = tok2->next )
{
if (tok2->str[0] == ')')
break;
@ -161,7 +161,7 @@ static void CheckBufferOverrun_LocalVariable()
_DynamicData();
int indentlevel = 0;
for (TOKEN *tok = tokens; tok; tok = tok->next)
for (const TOKEN *tok = tokens; tok; tok = tok->next)
{
if (tok->str[0]=='{')
indentlevel++;
@ -180,7 +180,7 @@ static void CheckBufferOverrun_LocalVariable()
if (total_size == 0)
continue;
int _indentlevel = indentlevel;
for (TOKEN *tok2 = gettok(tok,5); tok2; tok2 = tok2->next)
for (const TOKEN *tok2 = gettok(tok,5); tok2; tok2 = tok2->next)
{
if (tok2->str[0]=='{')
{
@ -265,7 +265,7 @@ static void CheckBufferOverrun_LocalVariable()
}
if (strindex && value>(int)size)
{
TOKEN *tok3 = tok2;
const TOKEN *tok3 = tok2;
while (tok3 && strcmp(tok3->str,")"))
tok3 = tok3->next;
if (!tok3)
@ -323,13 +323,13 @@ static void CheckBufferOverrun_LocalVariable()
}
//---------------------------------------------------------------------------
static void CheckBufferOverrun_StructVariable_CheckVar( TOKEN *tok1, const char varname[], const char dot[], const char arrname[], const int arrsize )
static void CheckBufferOverrun_StructVariable_CheckVar( const TOKEN *tok1, const char varname[], const char dot[], const char arrname[], const int arrsize )
{
const char *badpattern[] = {"varname",".","arrname","[","","]",NULL};
badpattern[0] = varname;
badpattern[1] = dot;
badpattern[2] = arrname;
TOKEN *tok2 = findtoken( tok1, badpattern );
const TOKEN *tok2 = findtoken( tok1, badpattern );
while (tok2)
{
if ( IsNumber( getstr(tok2, 4) ) )
@ -349,7 +349,7 @@ static void CheckBufferOverrun_StructVariable_CheckVar( TOKEN *tok1, const char
static void CheckBufferOverrun_StructVariable()
{
const char *declstruct_pattern[] = {"struct","","{",0};
for ( TOKEN * tok = findtoken( tokens, declstruct_pattern );
for ( const TOKEN * tok = findtoken( tokens, declstruct_pattern );
tok;
tok = findtoken( tok->next, declstruct_pattern ) )
{
@ -372,7 +372,7 @@ static void CheckBufferOverrun_StructVariable()
const char *arrname = getstr(tok2, 2);
const char *arrsize = getstr(tok2, 4);
for ( TOKEN *tok3 = tokens; tok3; tok3 = tok3->next )
for ( const TOKEN *tok3 = tokens; tok3; tok3 = tok3->next )
{
if ( strcmp(tok3->str, structname) )
continue;
@ -418,7 +418,7 @@ void CheckBufferOverrun()
void WarningDangerousFunctions()
{
for (TOKEN *tok = tokens; tok; tok = tok->next)
for (const TOKEN *tok = tokens; tok; tok = tok->next)
{
if (match(tok, "gets ("))
{

View File

@ -28,12 +28,12 @@ static struct VAR *ClassChecking_GetVarList(const char classname[])
// Locate class..
const char *pattern[] = {"class","","{",0};
pattern[1] = classname;
TOKEN *tok1 = findtoken(tokens, pattern);
const TOKEN *tok1 = findtoken(tokens, pattern);
// Get variable list..
struct VAR *varlist = NULL;
unsigned int indentlevel = 0;
for (TOKEN *tok = tok1; tok; tok = tok->next)
for (const TOKEN *tok = tok1; tok; tok = tok->next)
{
if (!tok->next)
break;
@ -50,7 +50,7 @@ static struct VAR *ClassChecking_GetVarList(const char classname[])
if (indentlevel==1 && (strchr(";{}", tok->str[0]) || (tok->str[0]!=':' && strchr(tok->str, ':'))))
{
TOKEN *next = tok->next;
const TOKEN *next = tok->next;
const char *varname = 0;
@ -90,7 +90,7 @@ static struct VAR *ClassChecking_GetVarList(const char classname[])
}
//---------------------------------------------------------------------------
static TOKEN * FindClassFunction( TOKEN *_tokens, const char classname[], const char funcname[], unsigned int &indentlevel )
static const TOKEN * FindClassFunction( const TOKEN *_tokens, const char classname[], const char funcname[], unsigned int &indentlevel )
{
while ( _tokens )
{
@ -105,7 +105,7 @@ static TOKEN * FindClassFunction( TOKEN *_tokens, const char classname[], const
// Member function is implemented in the class declaration..
if ( match( _tokens, "var (" ) && strcmp(_tokens->str,funcname) == 0 )
{
TOKEN *tok2 = _tokens;
const TOKEN *tok2 = _tokens;
while ( tok2 && tok2->str[0] != '{' && tok2->str[0] != ';' )
tok2 = tok2->next;
if ( tok2 && tok2->str[0] == '{' )
@ -135,7 +135,7 @@ static TOKEN * FindClassFunction( TOKEN *_tokens, const char classname[], const
}
//---------------------------------------------------------------------------
static void ClassChecking_VarList_Initialize(TOKEN *ftok, struct VAR *varlist, const char classname[])
static void ClassChecking_VarList_Initialize(const TOKEN *ftok, struct VAR *varlist, const char classname[])
{
bool BeginLine = false;
bool Assign = false;
@ -192,7 +192,7 @@ static void ClassChecking_VarList_Initialize(TOKEN *ftok, struct VAR *varlist, c
if (ftok->next->str[0] == '(')
{
unsigned int i = 0;
TOKEN *ftok2 = FindClassFunction( tokens, classname, ftok->str, i );
const TOKEN *ftok2 = FindClassFunction( tokens, classname, ftok->str, i );
ClassChecking_VarList_Initialize(ftok2, varlist, classname);
}
@ -246,7 +246,7 @@ void CheckConstructors()
{
// Locate class
const char *pattern_classname[] = {"class","","{",NULL};
TOKEN *tok1 = findtoken(tokens, pattern_classname);
const TOKEN *tok1 = findtoken(tokens, pattern_classname);
while (tok1)
{
const char *classname = tok1->next->str;
@ -259,7 +259,7 @@ void CheckConstructors()
// Are there a class constructor?
const char *constructor_pattern[] = {"","clKalle","(",NULL};
constructor_pattern[1] = classname;
TOKEN *constructor_token = findtoken( tokens, constructor_pattern );
const TOKEN *constructor_token = findtoken( tokens, constructor_pattern );
while ( constructor_token && constructor_token->str[0] == '~' )
constructor_token = findtoken( constructor_token->next, constructor_pattern );
if ( ! constructor_token )
@ -345,7 +345,7 @@ void CheckUnusedPrivateFunctions()
{
// Locate some class
const char *pattern_class[] = {"class","","{",NULL};
for (TOKEN *tok1 = findtoken(tokens, pattern_class); tok1; tok1 = findtoken(tok1->next, pattern_class))
for (const TOKEN *tok1 = findtoken(tokens, pattern_class); tok1; tok1 = findtoken(tok1->next, pattern_class))
{
const char *classname = tok1->next->str;
@ -361,7 +361,7 @@ void CheckUnusedPrivateFunctions()
FuncList.clear();
bool priv = false;
unsigned int indent_level = 0;
for (TOKEN *tok = tok1; tok; tok = tok->next)
for (const TOKEN *tok = tok1; tok; tok = tok->next)
{
if (match(tok,"friend class"))
{
@ -401,7 +401,7 @@ void CheckUnusedPrivateFunctions()
const char *pattern_function[] = {"","::",NULL};
pattern_function[0] = classname;
bool HasFuncImpl = false;
for (TOKEN *ftok = findtoken(tokens, pattern_function); ftok; ftok = findtoken(ftok->next,pattern_function))
for (const TOKEN *ftok = findtoken(tokens, pattern_function); ftok; ftok = findtoken(ftok->next,pattern_function))
{
int numpar = 0;
while (ftok && ftok->str[0]!=';' && ftok->str[0]!='{')
@ -474,7 +474,7 @@ void CheckUnusedPrivateFunctions()
void CheckMemset()
{
// Locate all 'memset' tokens..
for (TOKEN *tok = tokens; tok; tok = tok->next)
for (const TOKEN *tok = tokens; tok; tok = tok->next)
{
if (strcmp(tok->str,"memset")!=0)
continue;
@ -507,7 +507,7 @@ void CheckMemset()
// Warn if type is a struct that contains any std::*
const char *pattern2[] = {"struct","","{",NULL};
pattern2[1] = type;
for (TOKEN *tstruct = findtoken(tokens, pattern2); tstruct; tstruct = tstruct->next)
for (const TOKEN *tstruct = findtoken(tokens, pattern2); tstruct; tstruct = tstruct->next)
{
if (tstruct->str[0] == '}')
break;
@ -533,7 +533,7 @@ void CheckMemset()
void CheckOperatorEq1()
{
const char *pattern[] = {"void", "operator", "=", "(", NULL};
if (TOKEN *tok = findtoken(tokens,pattern))
if (const TOKEN *tok = findtoken(tokens,pattern))
{
std::ostringstream ostr;
ostr << FileLine(tok) << ": 'operator=' should return something";

View File

@ -16,7 +16,7 @@
void WarningOldStylePointerCast()
{
for (TOKEN *tok = tokens; tok; tok = tok->next)
for (const TOKEN *tok = tokens; tok; tok = tok->next)
{
// Old style pointer casting..
if (!match(tok, "( type * ) var"))
@ -43,7 +43,7 @@ void WarningOldStylePointerCast()
void WarningIsDigit()
{
for (TOKEN *tok = tokens; tok; tok = tok->next)
for (const TOKEN *tok = tokens; tok; tok = tok->next)
{
bool err = false;
err |= match(tok, "var >= '0' && var <= '9'");
@ -68,7 +68,7 @@ void WarningIsDigit()
void WarningIsAlpha()
{
for (TOKEN *tok = tokens; tok; tok = tok->next)
for (const TOKEN *tok = tokens; tok; tok = tok->next)
{
bool err = false;
@ -102,13 +102,13 @@ void WarningRedundantCode()
{
// if (p) delete p
for (TOKEN *tok = tokens; tok; tok = tok->next)
for (const TOKEN *tok = tokens; tok; tok = tok->next)
{
if (strcmp(tok->str,"if"))
continue;
const char *varname1 = NULL;
TOKEN *tok2 = NULL;
const TOKEN *tok2 = NULL;
if (match(tok,"if ( var )"))
{
@ -165,12 +165,12 @@ void WarningIf()
{
// Search for 'if (condition);'
for (TOKEN *tok = tokens; tok; tok = tok->next)
for (const TOKEN *tok = tokens; tok; tok = tok->next)
{
if (strcmp(tok->str,"if")==0)
{
int parlevel = 0;
for (TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next)
for (const TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next)
{
if (tok2->str[0]=='(')
parlevel++;
@ -194,7 +194,7 @@ void WarningIf()
}
// Search for 'a=b; if (a==b)'
for (TOKEN *tok = tokens; tok; tok = tok->next)
for (const TOKEN *tok = tokens; tok; tok = tok->next)
{
// Begin statement?
if ( ! strchr(";{}", tok->str[0]) )
@ -254,7 +254,7 @@ void WarningIf()
void InvalidFunctionUsage()
{
for ( TOKEN *tok = tokens; tok; tok = tok->next )
for ( const TOKEN *tok = tokens; tok; tok = tok->next )
{
if ( strcmp(tok->str, "strtol") && strcmp(tok->str, "strtoul") )
continue;
@ -262,7 +262,7 @@ void InvalidFunctionUsage()
// Locate the third parameter of the function call..
int parlevel = 0;
int param = 1;
for ( TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next )
for ( const TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next )
{
if ( tok2->str[0] == '(' )
parlevel++;
@ -298,12 +298,12 @@ void InvalidFunctionUsage()
// Dangerous usage of 'strtok'
//---------------------------------------------------------------------------
static TOKEN *GetFunction( TOKEN *content )
static const TOKEN *GetFunction( const TOKEN *content )
{
TOKEN *func = NULL;
const TOKEN *func = NULL;
int indentlevel = 0;
for (TOKEN *tok = tokens; tok; tok = tok->next)
for (const TOKEN *tok = tokens; tok; tok = tok->next)
{
if ( tok->str[0] == '{' )
indentlevel++;
@ -338,15 +338,15 @@ static TOKEN *GetFunction( TOKEN *content )
void WarningStrTok()
{
std::list<TOKEN *> funclist;
std::list<const TOKEN *> funclist;
// Which functions contain the 'strtok'?
for (TOKEN *tok = tokens; tok; tok = tok->next)
for (const TOKEN *tok = tokens; tok; tok = tok->next)
{
if (strcmp(tok->str,"strtok")!=0)
continue;
TOKEN *func = GetFunction(tok);
const TOKEN *func = GetFunction(tok);
if (!func)
continue;
@ -358,13 +358,13 @@ void WarningStrTok()
return;
// Take closer look at the strtok usage.
std::list<TOKEN *>::const_iterator it1;
std::list<const TOKEN *>::const_iterator it1;
for (it1 = funclist.begin(); it1 != funclist.end(); it1++)
{
// Search this function to check that it doesn't call any other of
// the functions in the funclist.
int indentlevel = 0;
for ( TOKEN *tok = *it1; tok; tok = tok->next )
for ( const TOKEN *tok = *it1; tok; tok = tok->next )
{
if ( tok->str[0] == '{' )
indentlevel++;
@ -383,7 +383,7 @@ void WarningStrTok()
continue;
// Check if function name is in funclist..
std::list<TOKEN *>::const_iterator it2;
std::list<const TOKEN *>::const_iterator it2;
for (it2 = funclist.begin(); it2 != funclist.end(); it2++)
{
if ( strcmp( tok->str, (*it2)->str ) )
@ -407,7 +407,7 @@ void WarningStrTok()
void CheckIfAssignment()
{
for (TOKEN *tok = tokens; tok; tok = tok->next)
for (const TOKEN *tok = tokens; tok; tok = tok->next)
{
if (match(tok,"if ( a = b )"))
{
@ -427,14 +427,14 @@ void CheckIfAssignment()
void CheckCaseWithoutBreak()
{
for ( TOKEN *tok = tokens; tok; tok = tok->next )
for ( const TOKEN *tok = tokens; tok; tok = tok->next )
{
if ( strcmp(tok->str,"case")!=0 )
continue;
// Found a case, check that there's a break..
int indentlevel = 0;
for (TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next)
for (const TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next)
{
if (tok2->str[0] == '{')
indentlevel++;
@ -548,12 +548,12 @@ void CheckVariableScope()
// Walk through all tokens..
bool func = false;
int indentlevel = 0;
for ( TOKEN *tok = tokens; tok; tok = tok->next )
for ( const TOKEN *tok = tokens; tok; tok = tok->next )
{
// Skip class and struct declarations..
if ( strcmp(tok->str, "class") == 0 || strcmp(tok->str, "struct") == 0 )
{
for (TOKEN *tok2 = tok; tok2; tok2 = tok2->next)
for (const TOKEN *tok2 = tok; tok2; tok2 = tok2->next)
{
if ( tok2->str[0] == '{' )
{
@ -603,7 +603,7 @@ void CheckVariableScope()
if ( indentlevel > 0 && func && strchr("{};", tok->str[0]) )
{
// First token of statement..
TOKEN *tok1 = tok->next;
const TOKEN *tok1 = tok->next;
if (strcmp(tok1->str,"return")==0 ||
strcmp(tok1->str,"delete")==0 ||

View File

@ -28,6 +28,18 @@ static void combine_2tokens(TOKEN *tok, const char str1[], const char str2[]);
static void DeleteNextToken(TOKEN *tok);
static TOKEN *_gettok(TOKEN *tok, int index)
{
while (tok && index>0)
{
tok = tok->next;
index--;
}
return tok;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
std::vector<std::string> Files;
@ -560,7 +572,7 @@ void SimplifyTokenList()
const char *sym = getstr(tok,2);
const char *num = getstr(tok,4);
for (TOKEN *tok2 = gettok(tok,6); tok2; tok2 = tok2->next)
for (TOKEN *tok2 = _gettok(tok,6); tok2; tok2 = tok2->next)
{
if (strcmp(tok2->str,sym) == 0)
{
@ -750,7 +762,7 @@ void SimplifyTokenList()
// Replace 'sizeof(var)' with number
int indentlevel = 0;
for ( TOKEN *tok2 = gettok(tok,5); tok2; tok2 = tok2->next )
for ( TOKEN *tok2 = _gettok(tok,5); tok2; tok2 = tok2->next )
{
if (tok2->str[0] == '{')
{
@ -885,56 +897,56 @@ void SimplifyTokenList()
if ( match(type0, "type var ,") )
{
tok2 = gettok(type0, 2); // The ',' token
tok2 = _gettok(type0, 2); // The ',' token
typelen = 1;
}
else if ( match(type0, "type * var ,") )
{
tok2 = gettok(type0, 3); // The ',' token
tok2 = _gettok(type0, 3); // The ',' token
typelen = 1;
}
else if ( match(type0, "type var [ num ] ,") )
{
tok2 = gettok(type0, 5); // The ',' token
tok2 = _gettok(type0, 5); // The ',' token
typelen = 1;
}
else if ( match(type0, "type * var [ num ] ,") )
{
tok2 = gettok(type0, 6); // The ',' token
tok2 = _gettok(type0, 6); // The ',' token
typelen = 1;
}
else if ( match(type0, "struct type var ,") )
{
tok2 = gettok(type0, 3);
tok2 = _gettok(type0, 3);
typelen = 2;
}
else if ( match(type0, "struct type * var ,") )
{
tok2 = gettok(type0, 4);
tok2 = _gettok(type0, 4);
typelen = 2;
}
else if ( match(type0, "type var =") )
{
tok2 = gettok(type0, 2);
tok2 = _gettok(type0, 2);
typelen = 1;
}
else if ( match(type0, "type * var =") )
{
tok2 = gettok(type0, 3);
tok2 = _gettok(type0, 3);
typelen = 1;
}
else if ( match(type0, "struct type * var =") )
{
tok2 = gettok(type0, 4);
tok2 = _gettok(type0, 4);
typelen = 2;
}
@ -969,7 +981,7 @@ void SimplifyTokenList()
else if ( parlevel==0 && strchr(";,",tok2->str[0]) )
{
// "type var =" => "type var; var ="
TOKEN *VarTok = gettok(type0,typelen);
TOKEN *VarTok = _gettok(type0,typelen);
if (VarTok->str[0]=='*')
VarTok = VarTok->next;
InsertTokens(eq, VarTok, 2);
@ -1008,12 +1020,12 @@ void SimplifyTokenList()
// Helper functions for handling the tokens list
//---------------------------------------------------------------------------
TOKEN *findtoken(TOKEN *tok1, const char *tokenstr[])
const TOKEN *findtoken(const TOKEN *tok1, const char *tokenstr[])
{
for (TOKEN *ret = tok1; ret; ret = ret->next)
for (const TOKEN *ret = tok1; ret; ret = ret->next)
{
unsigned int i = 0;
TOKEN *tok = ret;
const TOKEN *tok = ret;
while (tokenstr[i])
{
if (!tok)
@ -1073,7 +1085,7 @@ bool match(const TOKEN *tok, const char pattern[])
}
//---------------------------------------------------------------------------
TOKEN *gettok(TOKEN *tok, int index)
const TOKEN *gettok(const TOKEN *tok, int index)
{
while (tok && index>0)
{
@ -1084,7 +1096,7 @@ TOKEN *gettok(TOKEN *tok, int index)
}
//---------------------------------------------------------------------------
const char *getstr(TOKEN *tok, int index)
const char *getstr(const TOKEN *tok, int index)
{
tok = gettok(tok, index);
return tok ? tok->str : "";

View File

@ -35,10 +35,10 @@ void DeallocateTokens();
// Helper functions for handling the tokens list..
TOKEN *findtoken(TOKEN *tok1, const char *tokenstr[]);
const TOKEN *findtoken(const TOKEN *tok1, const char *tokenstr[]);
bool match(const TOKEN *tok, const char pattern[]);
TOKEN *gettok(TOKEN *tok, int index);
const char *getstr(TOKEN *tok, int index);
const TOKEN *gettok(const TOKEN *tok, int index);
const char *getstr(const TOKEN *tok, int index);
//---------------------------------------------------------------------------
#endif