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; struct VAR *var = new VAR;
memset(var, 0, sizeof(struct VAR)); memset(var, 0, sizeof(struct VAR));
var->name = varname; var->name = varname;
var->init = false;
var->next = varlist; var->next = varlist;
varlist = var; 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 // Locate class member function
const char *pattern[] = {"","::","","(",NULL}; const char *pattern[] = {"","::","","(",NULL};
@ -147,7 +148,7 @@ static TOKEN * ClassChecking_VarList_RemoveAssigned(TOKEN *_tokens, struct VAR *
// Calling member function? // Calling member function?
if (ftok->next->str[0] == '(') 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? // Assignment of member variable?
if (strcmp(ftok->next->str, "=") == 0) if (strcmp(ftok->next->str, "=") == 0)
@ -219,27 +220,35 @@ void CheckConstructors()
// Check that all member variables are initialized.. // Check that all member variables are initialized..
struct VAR *varlist = ClassChecking_GetVarList(classname); struct VAR *varlist = ClassChecking_GetVarList(classname);
ClassChecking_VarList_RemoveAssigned(tokens, varlist, classname, classname);
// Check if any variables are uninitialized const char *constructor_pattern[] = {"","::","","(",NULL};
for (struct VAR *var = varlist; var; var = var->next) constructor_pattern[0] = classname;
constructor_pattern[2] = classname;
struct TOKEN *constructor_token = ClassChecking_VarList_Initialize(tokens, varlist, classname, classname);
while ( constructor_token )
{ {
// Is it a static member variable? // Check if any variables are uninitialized
const char *pattern[] = {"","::","","=",NULL}; for (struct VAR *var = varlist; var; var = var->next)
pattern[0] = classname;
pattern[2] = var->name;
// Locate member function implementation..
TOKEN *ftok = findtoken(tokens, pattern);
if (ftok)
continue;
if (!var->init)
{ {
std::ostringstream ostr; // Is it a static member variable?
ostr << "Uninitialized member variable '" << classname << "::" << var->name << "'"; const char *pattern[] = {"","::","","=",NULL};
ReportErr(ostr.str()); pattern[0] = classname;
pattern[2] = var->name;
if (findtoken(tokens, pattern))
continue;
if (!var->init)
{
std::ostringstream ostr;
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.. // Delete the 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() Fred::Fred()
{ } {
_i = 0;
}
Fred::Fred(int i) Fred::Fred(int i)
{ } { }

View File

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