2007-05-25 08:51:18 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#include "CheckOther.h"
|
2007-07-17 08:15:50 +02:00
|
|
|
#include "tokenize.h"
|
2007-05-25 08:51:18 +02:00
|
|
|
#include "CommonCheck.h"
|
2007-06-05 22:29:03 +02:00
|
|
|
#include <list>
|
2007-05-25 08:51:18 +02:00
|
|
|
#include <sstream>
|
2007-06-05 20:58:27 +02:00
|
|
|
#include <stdlib.h> // <- atoi
|
2007-05-25 08:51:18 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Warning on C-Style casts.. p = (kalle *)foo;
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void WarningOldStylePointerCast()
|
|
|
|
{
|
2008-03-23 14:38:01 +01:00
|
|
|
for (const TOKEN *tok = tokens; tok; tok = tok->next)
|
2007-05-25 08:51:18 +02:00
|
|
|
{
|
|
|
|
// Old style pointer casting..
|
2008-03-28 08:18:03 +01:00
|
|
|
if (!Match(tok, "( %type% * ) %var%"))
|
2007-05-25 08:51:18 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Is "type" a class?
|
|
|
|
const char *pattern[] = {"class","",NULL};
|
|
|
|
pattern[1] = getstr(tok, 1);
|
|
|
|
if (!findtoken(tokens, pattern))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << FileLine(tok) << ": C-style pointer casting";
|
|
|
|
ReportErr(ostr.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Use standard function "isdigit" instead
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void WarningIsDigit()
|
|
|
|
{
|
2008-03-22 12:46:06 +01:00
|
|
|
for (const TOKEN *tok = tokens; tok; tok = tok->next)
|
2007-05-25 08:51:18 +02:00
|
|
|
{
|
|
|
|
bool err = false;
|
2008-03-28 08:18:03 +01:00
|
|
|
err |= Match(tok, "%var% >= '0' && %var% <= '9'");
|
|
|
|
err |= Match(tok, "* %var% >= '0' && * %var% <= '9'");
|
|
|
|
err |= Match(tok, "( %var% >= '0' ) && ( %var% <= '9' )");
|
|
|
|
err |= Match(tok, "( * %var% >= '0' ) && ( * %var% <= '9' )");
|
2007-05-25 08:51:18 +02:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << FileLine(tok) << ": The condition can be simplified; use 'isdigit'";
|
|
|
|
ReportErr(ostr.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2007-06-05 06:51:01 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Use standard function "isalpha" instead
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void WarningIsAlpha()
|
|
|
|
{
|
2008-03-22 12:46:06 +01:00
|
|
|
for (const TOKEN *tok = tokens; tok; tok = tok->next)
|
2007-06-05 06:51:01 +02:00
|
|
|
{
|
|
|
|
bool err = false;
|
|
|
|
|
|
|
|
if ( tok->str[0] != '(' )
|
|
|
|
continue;
|
|
|
|
|
2008-03-28 08:18:03 +01:00
|
|
|
err |= Match(tok, "( %var% >= 'A' && %var% <= 'Z' ) || ( %var% >= 'a' && %var% <= 'z' )");
|
|
|
|
err |= Match(tok, "( %var% >= 'a' && %var% <= 'z' ) || ( %var% >= 'A' && %var% <= 'Z' )");
|
|
|
|
err |= Match(tok, "( * %var% >= 'A' && * %var% <= 'Z' ) || ( * %var% >= 'a' && * %var% <= 'z' )");
|
|
|
|
err |= Match(tok, "( * %var% >= 'a' && * %var% <= 'z' ) || ( * %var% >= 'A' && * %var% <= 'Z' )");
|
|
|
|
err |= Match(tok, "( ( %var% >= 'A' ) && ( %var% <= 'Z' ) ) || ( ( %var% >= 'a' ) && ( %var% <= 'z' ) )");
|
|
|
|
err |= Match(tok, "( ( %var% >= 'a' ) && ( %var% <= 'z' ) ) || ( ( %var% >= 'A' ) && ( %var% <= 'Z' ) )");
|
|
|
|
err |= Match(tok, "( ( * %var% >= 'A' ) && ( * %var% <= 'Z' ) ) || ( ( * var >= 'a' ) && ( * %var% <= 'z' ) )");
|
|
|
|
err |= Match(tok, "( ( * %var% >= 'a' ) && ( * %var% <= 'z' ) ) || ( ( * var >= 'A' ) && ( * %var% <= 'Z' ) )");
|
2007-06-05 06:51:01 +02:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << FileLine(tok) << ": The condition can be simplified; use 'isalpha'";
|
|
|
|
ReportErr(ostr.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2007-05-25 08:51:18 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Redundant code..
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void WarningRedundantCode()
|
|
|
|
{
|
|
|
|
|
|
|
|
// if (p) delete p
|
2008-03-22 12:46:06 +01:00
|
|
|
for (const TOKEN *tok = tokens; tok; tok = tok->next)
|
2007-05-25 08:51:18 +02:00
|
|
|
{
|
|
|
|
if (strcmp(tok->str,"if"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const char *varname1 = NULL;
|
2008-03-22 12:46:06 +01:00
|
|
|
const TOKEN *tok2 = NULL;
|
2007-05-25 08:51:18 +02:00
|
|
|
|
2008-03-28 08:18:03 +01:00
|
|
|
if (Match(tok,"if ( %var% )"))
|
2007-05-25 08:51:18 +02:00
|
|
|
{
|
|
|
|
varname1 = getstr(tok, 2);
|
|
|
|
tok2 = gettok(tok, 4);
|
|
|
|
}
|
2008-03-28 08:18:03 +01:00
|
|
|
else if (Match(tok,"if ( %var% != NULL )"))
|
2007-05-25 08:51:18 +02:00
|
|
|
{
|
|
|
|
varname1 = getstr(tok, 2);
|
|
|
|
tok2 = gettok(tok, 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (varname1==NULL || tok2==NULL)
|
|
|
|
continue;
|
|
|
|
|
2008-05-14 06:30:56 +02:00
|
|
|
if ( tok2->str[0] == '{' )
|
|
|
|
tok2 = tok2->next;
|
|
|
|
|
2007-05-25 08:51:18 +02:00
|
|
|
bool err = false;
|
2008-03-28 08:18:03 +01:00
|
|
|
if (Match(tok2,"delete %var% ;"))
|
2007-05-25 08:51:18 +02:00
|
|
|
err = (strcmp(getstr(tok2,1),varname1)==0);
|
2008-03-28 08:18:03 +01:00
|
|
|
else if (Match(tok2,"delete [ ] %var% ;"))
|
2007-05-25 08:51:18 +02:00
|
|
|
err = (strcmp(getstr(tok2,1),varname1)==0);
|
2008-03-28 08:18:03 +01:00
|
|
|
else if (Match(tok2,"free ( %var% )"))
|
2007-05-25 08:51:18 +02:00
|
|
|
err = (strcmp(getstr(tok2,2),varname1)==0);
|
2008-05-14 06:30:56 +02:00
|
|
|
else if (Match(tok2,"kfree ( %var% )"))
|
|
|
|
err = (strcmp(getstr(tok2,2),varname1)==0);
|
2007-05-25 08:51:18 +02:00
|
|
|
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << FileLine(tok) << ": Redundant condition. It is safe to deallocate a NULL pointer";
|
|
|
|
ReportErr(ostr.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-28 08:18:03 +01:00
|
|
|
// TODO: Redundant condition
|
2007-05-25 08:51:18 +02:00
|
|
|
// if (haystack.find(needle) != haystack.end())
|
|
|
|
// haystack.remove(needle);
|
|
|
|
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
2007-06-08 18:46:51 +02:00
|
|
|
// if (condition) ....
|
2007-05-25 08:51:18 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void WarningIf()
|
|
|
|
{
|
2007-06-02 18:29:05 +02:00
|
|
|
|
|
|
|
// Search for 'if (condition);'
|
2008-03-22 12:46:06 +01:00
|
|
|
for (const TOKEN *tok = tokens; tok; tok = tok->next)
|
2007-05-25 08:51:18 +02:00
|
|
|
{
|
|
|
|
if (strcmp(tok->str,"if")==0)
|
|
|
|
{
|
|
|
|
int parlevel = 0;
|
2008-03-22 12:46:06 +01:00
|
|
|
for (const TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next)
|
2007-05-25 08:51:18 +02:00
|
|
|
{
|
|
|
|
if (tok2->str[0]=='(')
|
|
|
|
parlevel++;
|
|
|
|
else if (tok2->str[0]==')')
|
|
|
|
{
|
|
|
|
parlevel--;
|
|
|
|
if (parlevel<=0)
|
|
|
|
{
|
2007-06-02 18:29:05 +02:00
|
|
|
if (strcmp(getstr(tok2,1), ";") == 0 &&
|
|
|
|
strcmp(getstr(tok2,2), "else") != 0)
|
2007-05-25 08:51:18 +02:00
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << FileLine(tok) << ": Found \"if (condition);\"";
|
|
|
|
ReportErr(ostr.str());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-06-02 18:29:05 +02:00
|
|
|
|
|
|
|
// Search for 'a=b; if (a==b)'
|
2008-03-23 14:38:01 +01:00
|
|
|
for (const TOKEN *tok = tokens; tok; tok = tok->next)
|
2007-06-02 18:29:05 +02:00
|
|
|
{
|
2008-03-23 14:38:01 +01:00
|
|
|
// Begin statement?
|
|
|
|
if ( ! strchr(";{}", tok->str[0]) )
|
|
|
|
continue;
|
|
|
|
tok = tok->next;
|
|
|
|
if ( ! tok )
|
|
|
|
break;
|
|
|
|
|
2008-03-28 08:18:03 +01:00
|
|
|
if (!Match(tok,"%var% = %var% ; if ( %var%"))
|
2007-06-02 18:29:05 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if ( strcmp(getstr(tok, 9), ")") != 0 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// var1 = var2 ; if ( var3 cond var4 )
|
|
|
|
const char *var1 = tok->str;
|
|
|
|
const char *var2 = getstr(tok, 2);
|
|
|
|
const char *var3 = getstr(tok, 6);
|
|
|
|
const char *cond = getstr(tok, 7);
|
|
|
|
const char *var4 = getstr(tok, 8);
|
|
|
|
|
|
|
|
// Check that var3 is equal with either var1 or var2
|
|
|
|
if (strcmp(var1,var3) && strcmp(var2,var3))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check that var4 is equal with either var1 or var2
|
|
|
|
if (strcmp(var1,var4) && strcmp(var2,var4))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check that there is a condition..
|
|
|
|
const char *p[6] = {"==","<=",">=","!=","<",">"};
|
|
|
|
bool iscond = false;
|
|
|
|
for (int i = 0; i < 6; i++)
|
|
|
|
iscond |= (strcmp(cond, p[i]) == 0);
|
|
|
|
if (!iscond)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// we found the error. Report.
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << FileLine(gettok(tok,4)) << ": The condition is always ";
|
|
|
|
for (int i = 0; i < 6; i++)
|
|
|
|
{
|
|
|
|
if (strcmp(cond, p[i]) == 0)
|
|
|
|
ostr << (i < 3 ? "True" : "False");
|
|
|
|
}
|
|
|
|
ReportErr(ostr.str());
|
2008-05-14 06:30:56 +02:00
|
|
|
}
|
2007-05-25 08:51:18 +02:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2007-06-05 20:58:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// strtol(str, 0, radix) <- radix must be 0 or 2-36
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void InvalidFunctionUsage()
|
|
|
|
{
|
2008-03-22 12:46:06 +01:00
|
|
|
for ( const TOKEN *tok = tokens; tok; tok = tok->next )
|
2007-06-05 20:58:27 +02:00
|
|
|
{
|
|
|
|
if ( strcmp(tok->str, "strtol") && strcmp(tok->str, "strtoul") )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Locate the third parameter of the function call..
|
|
|
|
int parlevel = 0;
|
|
|
|
int param = 1;
|
2008-03-22 12:46:06 +01:00
|
|
|
for ( const TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next )
|
2007-06-05 20:58:27 +02:00
|
|
|
{
|
|
|
|
if ( tok2->str[0] == '(' )
|
|
|
|
parlevel++;
|
|
|
|
else if (tok2->str[0] == ')')
|
|
|
|
parlevel--;
|
|
|
|
else if (parlevel == 1 && tok2->str[0] == ',')
|
|
|
|
{
|
|
|
|
param++;
|
|
|
|
if (param==3)
|
|
|
|
{
|
2008-03-28 08:18:03 +01:00
|
|
|
if ( Match(tok2, ", %num% )") )
|
2007-06-05 20:58:27 +02:00
|
|
|
{
|
|
|
|
int radix = atoi(tok2->next->str);
|
|
|
|
if (!(radix==0 || (radix>=2 && radix<=36)))
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << FileLine(tok2) << ": Invalid radix in call to strtol or strtoul. Must be 0 or 2-36";
|
|
|
|
ReportErr(ostr.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-06-05 22:29:03 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2007-07-24 08:23:28 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Assignment in condition
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CheckIfAssignment()
|
|
|
|
{
|
2008-03-22 12:46:06 +01:00
|
|
|
for (const TOKEN *tok = tokens; tok; tok = tok->next)
|
2007-07-24 08:23:28 +02:00
|
|
|
{
|
2008-03-28 08:18:03 +01:00
|
|
|
if (Match(tok, "if ( %var% = %num% )") ||
|
|
|
|
Match(tok, "if ( %var% = %str% )") ||
|
|
|
|
Match(tok, "if ( %var% = %var% )") )
|
2007-07-24 08:23:28 +02:00
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << FileLine(tok) << ": Possible bug. Should it be '==' instead of '='?";
|
|
|
|
ReportErr(ostr.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2008-02-20 19:20:59 +01:00
|
|
|
//---------------------------------------------------------------------------
|
2008-02-22 08:26:23 +01:00
|
|
|
// Check for unsigned divisions
|
2008-02-20 19:20:59 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CheckUnsignedDivision()
|
|
|
|
{
|
2008-03-20 08:23:01 +01:00
|
|
|
// Check for "ivar / uvar" and "uvar / ivar"
|
|
|
|
// Todo: Much better checking for declared variables
|
|
|
|
const char *div_pattern[] = {"", "/", "", NULL};
|
|
|
|
for (const TOKEN *div_tok = findtoken(tokens, div_pattern); div_tok; div_tok = findtoken(div_tok->next, div_pattern))
|
2008-02-20 19:20:59 +01:00
|
|
|
{
|
2008-03-20 08:23:01 +01:00
|
|
|
const char *varname1 = div_tok->str;
|
|
|
|
const char *varname2 = div_tok->next->next->str;
|
|
|
|
if ( IsName(varname1) && IsName(varname2) )
|
2008-02-20 19:20:59 +01:00
|
|
|
{
|
2008-03-20 08:23:01 +01:00
|
|
|
char var1_sign=0, var2_sign=0;
|
2008-02-20 19:20:59 +01:00
|
|
|
|
2008-03-20 08:23:01 +01:00
|
|
|
// Check if any of the variables are unsigned..
|
|
|
|
const char *pattern_declvar[] = { "unsigned", "", "", NULL };
|
2008-02-20 19:20:59 +01:00
|
|
|
|
2008-03-20 08:23:01 +01:00
|
|
|
pattern_declvar[2] = varname1;
|
|
|
|
if ( findtoken(tokens, pattern_declvar) )
|
|
|
|
var1_sign = 'u';
|
|
|
|
|
|
|
|
pattern_declvar[2] = varname2;
|
|
|
|
if ( findtoken(tokens, pattern_declvar) )
|
|
|
|
var2_sign = 'u';
|
|
|
|
|
|
|
|
if (var1_sign == var2_sign)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check if any of the variables are signed..
|
|
|
|
pattern_declvar[0] = ";";
|
|
|
|
pattern_declvar[1] = "int";
|
|
|
|
|
2008-03-21 07:44:52 +01:00
|
|
|
pattern_declvar[2] = varname1;
|
|
|
|
if ( findtoken(tokens, pattern_declvar) )
|
|
|
|
var1_sign = 's';
|
|
|
|
|
|
|
|
pattern_declvar[2] = varname2;
|
|
|
|
if ( findtoken(tokens, pattern_declvar) )
|
|
|
|
var2_sign = 's';
|
|
|
|
|
|
|
|
pattern_declvar[0] = "{";
|
|
|
|
|
2008-03-20 08:23:01 +01:00
|
|
|
pattern_declvar[2] = varname1;
|
|
|
|
if ( findtoken(tokens, pattern_declvar) )
|
|
|
|
var1_sign = 's';
|
|
|
|
|
|
|
|
pattern_declvar[2] = varname2;
|
|
|
|
if ( findtoken(tokens, pattern_declvar) )
|
|
|
|
var2_sign = 's';
|
|
|
|
|
|
|
|
if ( var1_sign && var2_sign && var1_sign != var2_sign )
|
2008-02-20 19:20:59 +01:00
|
|
|
{
|
2008-03-20 08:23:01 +01:00
|
|
|
// One of the operands are signed, the other is unsigned..
|
2008-02-20 19:20:59 +01:00
|
|
|
std::ostringstream ostr;
|
2008-03-20 08:23:01 +01:00
|
|
|
ostr << FileLine(div_tok) << ": If the result is negative it will be wrong because an operand is unsigned.";
|
2008-02-20 19:20:59 +01:00
|
|
|
ReportErr(ostr.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-03-16 14:17:43 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Check scope of variables..
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[] );
|
|
|
|
|
|
|
|
void CheckVariableScope()
|
|
|
|
{
|
|
|
|
// Walk through all tokens..
|
|
|
|
bool func = false;
|
|
|
|
int indentlevel = 0;
|
2008-03-22 12:46:06 +01:00
|
|
|
for ( const TOKEN *tok = tokens; tok; tok = tok->next )
|
2008-03-16 14:17:43 +01:00
|
|
|
{
|
2008-03-21 07:44:52 +01:00
|
|
|
// Skip class and struct declarations..
|
|
|
|
if ( strcmp(tok->str, "class") == 0 || strcmp(tok->str, "struct") == 0 )
|
|
|
|
{
|
2008-03-22 12:46:06 +01:00
|
|
|
for (const TOKEN *tok2 = tok; tok2; tok2 = tok2->next)
|
|
|
|
{
|
2008-03-21 07:44:52 +01:00
|
|
|
if ( tok2->str[0] == '{' )
|
|
|
|
{
|
|
|
|
int _indentlevel = 0;
|
|
|
|
tok = tok2;
|
|
|
|
for (tok = tok2; tok; tok = tok->next)
|
|
|
|
{
|
2008-03-23 14:38:01 +01:00
|
|
|
if ( tok->str[0] == '{' )
|
|
|
|
{
|
|
|
|
_indentlevel++;
|
|
|
|
}
|
|
|
|
if ( tok->str[0] == '}' )
|
2008-03-21 07:44:52 +01:00
|
|
|
{
|
2008-03-23 14:38:01 +01:00
|
|
|
_indentlevel--;
|
|
|
|
if ( _indentlevel <= 0 )
|
|
|
|
{
|
|
|
|
tok = tok->next;
|
|
|
|
break;
|
|
|
|
}
|
2008-03-21 07:44:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (strchr(",);", tok2->str[0]))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( ! tok )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-03-23 14:38:01 +01:00
|
|
|
if ( tok->str[0] == '{' )
|
2008-03-16 14:17:43 +01:00
|
|
|
{
|
2008-03-23 14:38:01 +01:00
|
|
|
indentlevel++;
|
|
|
|
}
|
|
|
|
if ( tok->str[0] == '}' )
|
|
|
|
{
|
|
|
|
indentlevel--;
|
|
|
|
if ( indentlevel == 0 )
|
|
|
|
func = false;
|
2008-03-16 14:17:43 +01:00
|
|
|
}
|
2008-03-28 08:18:03 +01:00
|
|
|
if ( indentlevel == 0 && Match(tok, ") {") )
|
2008-03-16 14:17:43 +01:00
|
|
|
{
|
|
|
|
func = true;
|
|
|
|
}
|
|
|
|
if ( indentlevel > 0 && func && strchr("{};", tok->str[0]) )
|
|
|
|
{
|
2008-03-16 18:39:38 +01:00
|
|
|
// First token of statement..
|
2008-03-22 12:46:06 +01:00
|
|
|
const TOKEN *tok1 = tok->next;
|
2008-03-29 18:27:02 +01:00
|
|
|
if ( ! tok1 )
|
|
|
|
continue;
|
2008-03-16 18:39:38 +01:00
|
|
|
|
|
|
|
if (strcmp(tok1->str,"return")==0 ||
|
|
|
|
strcmp(tok1->str,"delete")==0 ||
|
|
|
|
strcmp(tok1->str,"else")==0)
|
|
|
|
continue;
|
2008-03-16 14:17:43 +01:00
|
|
|
|
|
|
|
// Variable declaration?
|
2008-03-28 08:18:03 +01:00
|
|
|
if (Match(tok1, "%var% %var% ;") ||
|
|
|
|
Match(tok1, "%var% %var% =") )
|
2008-03-16 18:39:38 +01:00
|
|
|
{
|
|
|
|
CheckVariableScope_LookupVar( tok1, getstr(tok1, 1) );
|
|
|
|
}
|
2008-03-16 14:17:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[] )
|
|
|
|
{
|
|
|
|
const TOKEN *tok = tok1;
|
|
|
|
|
|
|
|
// Skip the variable declaration..
|
|
|
|
while ( tok->str[0] != ';' )
|
|
|
|
tok = tok->next;
|
|
|
|
|
|
|
|
// Check if the variable is used in this indentlevel..
|
2008-03-17 12:05:30 +01:00
|
|
|
bool used = false, used1 = false;
|
2008-03-23 08:57:27 +01:00
|
|
|
int indentlevel = 0;
|
2008-04-01 08:21:12 +02:00
|
|
|
int parlevel = 0;
|
2008-03-23 14:38:01 +01:00
|
|
|
bool for_or_while = false;
|
|
|
|
while ( indentlevel >= 0 && tok )
|
2008-03-16 14:17:43 +01:00
|
|
|
{
|
2008-03-23 14:38:01 +01:00
|
|
|
if ( tok->str[0] == '{' )
|
2008-03-16 14:17:43 +01:00
|
|
|
{
|
2008-03-23 14:38:01 +01:00
|
|
|
indentlevel++;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ( tok->str[0] == '}' )
|
|
|
|
{
|
|
|
|
indentlevel--;
|
|
|
|
if ( indentlevel == 0 )
|
|
|
|
{
|
|
|
|
if ( for_or_while && used )
|
|
|
|
return;
|
|
|
|
used1 = used;
|
|
|
|
used = false;
|
|
|
|
}
|
2008-03-16 14:17:43 +01:00
|
|
|
}
|
|
|
|
|
2008-04-01 08:21:12 +02:00
|
|
|
else if ( tok->str[0] == '(' )
|
|
|
|
{
|
|
|
|
parlevel++;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ( tok->str[0] == ')' )
|
|
|
|
{
|
|
|
|
parlevel--;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-16 14:17:43 +01:00
|
|
|
else if ( strcmp(tok->str, varname) == 0 )
|
|
|
|
{
|
2008-03-17 12:05:30 +01:00
|
|
|
if ( indentlevel == 0 || used1 )
|
2008-03-16 14:17:43 +01:00
|
|
|
return;
|
|
|
|
used = true;
|
|
|
|
}
|
|
|
|
|
2008-03-21 07:44:52 +01:00
|
|
|
else if ( indentlevel==0 )
|
|
|
|
{
|
|
|
|
if ( strcmp(tok->str,"for")==0 || strcmp(tok->str,"while")==0 )
|
|
|
|
for_or_while = true;
|
2008-04-01 08:21:12 +02:00
|
|
|
if ( parlevel == 0 && tok->str[0] == ';' )
|
2008-03-21 07:44:52 +01:00
|
|
|
for_or_while = false;
|
|
|
|
}
|
2008-03-23 14:38:01 +01:00
|
|
|
|
|
|
|
tok = tok->next;
|
2008-03-16 14:17:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Warning if "used" is true
|
|
|
|
std::ostringstream errmsg;
|
|
|
|
errmsg << FileLine(tok1) << " The scope of the variable '" << varname << "' can be limited";
|
|
|
|
ReportErr( errmsg.str() );
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
2008-05-03 09:20:25 +02:00
|
|
|
|
2008-03-16 14:17:43 +01:00
|
|
|
|
2007-07-24 08:23:28 +02:00
|
|
|
|
2008-05-03 09:20:25 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Check for constant function parameters
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CheckConstantFunctionParameter()
|
|
|
|
{
|
|
|
|
for (const TOKEN *tok = tokens; tok; tok = tok->next)
|
|
|
|
{
|
2008-05-03 11:44:20 +02:00
|
|
|
if ( Match(tok,"[,(] const std :: %type% %var% [,)]") )
|
2008-05-03 09:20:25 +02:00
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
2008-05-03 11:44:20 +02:00
|
|
|
errmsg << FileLine(tok) << " " << getstr(tok,5) << " is passed by value, it could be passed by reference/pointer instead";
|
2008-05-03 09:20:25 +02:00
|
|
|
ReportErr( errmsg.str() );
|
|
|
|
}
|
2008-05-03 11:44:20 +02:00
|
|
|
|
|
|
|
else if ( Match(tok,"[,(] const %type% %var% [,)]") )
|
|
|
|
{
|
|
|
|
// Check if type is a struct or class.
|
|
|
|
const char *pattern[3] = {"class","type",0};
|
|
|
|
pattern[1] = getstr(tok, 2);
|
|
|
|
if ( findtoken(tokens, pattern) )
|
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
|
|
|
errmsg << FileLine(tok) << " " << getstr(tok,3) << " is passed by value, it could be passed by reference/pointer instead";
|
|
|
|
ReportErr( errmsg.str() );
|
|
|
|
}
|
|
|
|
pattern[0] = "struct";
|
|
|
|
if ( findtoken(tokens, pattern) )
|
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
|
|
|
errmsg << FileLine(tok) << " " << getstr(tok,3) << " is passed by value, it could be passed by reference/pointer instead";
|
|
|
|
ReportErr( errmsg.str() );
|
|
|
|
}
|
|
|
|
}
|
2008-05-03 09:20:25 +02:00
|
|
|
}
|
|
|
|
}
|
2008-05-09 20:29:42 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Check that all struct members are used
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CheckStructMemberUsage()
|
|
|
|
{
|
|
|
|
const char *structname = 0;
|
|
|
|
|
|
|
|
for ( const TOKEN *tok = tokens; tok; tok = tok->next )
|
|
|
|
{
|
|
|
|
if ( tok->FileIndex != 0 )
|
|
|
|
continue;
|
|
|
|
if ( tok->str[0] == '}' )
|
|
|
|
structname = 0;
|
|
|
|
if ( Match(tok, "struct %type% {") )
|
|
|
|
structname = getstr(tok, 1);
|
|
|
|
|
|
|
|
if (structname && Match(tok, "[{;]"))
|
|
|
|
{
|
|
|
|
const char *varname = 0;
|
2008-05-10 08:48:37 +02:00
|
|
|
if (Match(tok->next, "%type% %var% [;[]"))
|
2008-05-09 20:29:42 +02:00
|
|
|
varname = getstr( tok, 2 );
|
2008-05-10 08:48:37 +02:00
|
|
|
else if (Match(tok->next, "%type% %type% %var% [;[]"))
|
|
|
|
varname = getstr( tok, 2 );
|
|
|
|
else if (Match(tok->next, "%type% * %var% [;[]"))
|
|
|
|
varname = getstr( tok, 3 );
|
|
|
|
else if (Match(tok->next, "%type% %type% * %var% [;[]"))
|
|
|
|
varname = getstr( tok, 4 );
|
2008-05-09 20:29:42 +02:00
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const char *varnames[2];
|
|
|
|
varnames[0] = varname;
|
|
|
|
varnames[1] = 0;
|
|
|
|
bool used = false;
|
|
|
|
for ( const TOKEN *tok2 = tokens; tok2; tok2 = tok2->next )
|
|
|
|
{
|
|
|
|
if ( tok->FileIndex != 0 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Match(tok2, ". %var%", varnames))
|
|
|
|
{
|
|
|
|
if ( strcmp("=", getstr(tok2,2)) == 0 )
|
|
|
|
continue;
|
|
|
|
used = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! used )
|
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
|
|
|
errmsg << FileLine(tok) << ": struct member '" << structname << "::" << varname << "' is never read";
|
|
|
|
ReportErr(errmsg.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-06-05 22:29:03 +02:00
|
|
|
|