Better checking of uninitialized variables

This commit is contained in:
Daniel Marjamäki 2008-01-17 07:46:10 +00:00
parent f3a96109eb
commit a8e6a4c343
4 changed files with 33 additions and 22 deletions

View File

@ -72,6 +72,7 @@ static struct VAR *ClassChecking_GetVarList(const char classname[])
struct VAR *var = new VAR;
memset(var, 0, sizeof(struct VAR));
var->name = varname;
var->init = false;
var->next = varlist;
varlist = var;
}
@ -82,7 +83,7 @@ static struct VAR *ClassChecking_GetVarList(const char classname[])
}
//---------------------------------------------------------------------------
static TOKEN * ClassChecking_VarList_RemoveAssigned(TOKEN *_tokens, struct VAR *varlist, const char classname[], const char funcname[])
static TOKEN * ClassChecking_VarList_Initialize(TOKEN *_tokens, struct VAR *varlist, const char classname[], const char funcname[])
{
// Locate class member function
const char *pattern[] = {"","::","","(",NULL};
@ -147,7 +148,7 @@ static TOKEN * ClassChecking_VarList_RemoveAssigned(TOKEN *_tokens, struct VAR *
// Calling member function?
if (ftok->next->str[0] == '(')
ClassChecking_VarList_RemoveAssigned(tokens, varlist, classname, ftok->str);
ClassChecking_VarList_Initialize(tokens, varlist, classname, ftok->str);
// Assignment of member variable?
if (strcmp(ftok->next->str, "=") == 0)
@ -219,8 +220,13 @@ void CheckConstructors()
// Check that all member variables are initialized..
struct VAR *varlist = ClassChecking_GetVarList(classname);
ClassChecking_VarList_RemoveAssigned(tokens, varlist, classname, classname);
const char *constructor_pattern[] = {"","::","","(",NULL};
constructor_pattern[0] = classname;
constructor_pattern[2] = classname;
struct TOKEN *constructor_token = ClassChecking_VarList_Initialize(tokens, varlist, classname, classname);
while ( constructor_token )
{
// Check if any variables are uninitialized
for (struct VAR *var = varlist; var; var = var->next)
{
@ -228,20 +234,23 @@ void CheckConstructors()
const char *pattern[] = {"","::","","=",NULL};
pattern[0] = classname;
pattern[2] = var->name;
// Locate member function implementation..
TOKEN *ftok = findtoken(tokens, pattern);
if (ftok)
if (findtoken(tokens, pattern))
continue;
if (!var->init)
{
std::ostringstream ostr;
ostr << "Uninitialized member variable '" << classname << "::" << var->name << "'";
ostr << FileLine(constructor_token);
ostr << " Uninitialized member variable '" << classname << "::" << var->name << "'";
ReportErr(ostr.str());
}
}
for ( struct VAR *var = varlist; var; var = var->next )
var->init = false;
constructor_token = ClassChecking_VarList_Initialize(constructor_token->next, varlist, classname, classname);
}
// Delete the varlist..
while (varlist)
{

View File

@ -1 +1 @@
Uninitialized member variable 'Fred::_i'
[testUninitVar1\testUninitVar1.cpp:17] Uninitialized member variable 'Fred::_i'

View File

@ -9,7 +9,9 @@ public:
}
Fred::Fred()
{ }
{
_i = 0;
}
Fred::Fred(int i)
{ }

View File

@ -1 +1 @@
Uninitialized member variable 'clKalle::i'
[testclass13\testclass13.cpp:14] Uninitialized member variable 'clKalle::i'