CheckStructMemberUsage: Check for unused struct members

This commit is contained in:
Daniel Marjamäki 2008-05-09 18:29:42 +00:00
parent 7464adfb47
commit 219d82e180
4 changed files with 77 additions and 1 deletions

View File

@ -732,4 +732,60 @@ void CheckConstantFunctionParameter()
}
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 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;
if (Match(tok->next, "%type% %var% ;"))
varname = getstr( tok, 2 );
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());
}
}
}
}

View File

@ -43,6 +43,9 @@ void CheckVariableScope();
// Check for constant function parameter
void CheckConstantFunctionParameter();
// Check that all struct members are used
void CheckStructMemberUsage();
//---------------------------------------------------------------------------
#endif

View File

@ -300,7 +300,10 @@ static void CppCheck(const char FileName[], unsigned int FileId)
CheckVariableScope();
// Check if a constant function parameter is passed by value
CheckConstantFunctionParameter();
CheckConstantFunctionParameter();
// Unused struct members..
CheckStructMemberUsage();
}
// Clean up tokens..

View File

@ -29,6 +29,7 @@ static void memleak_in_class();
static void division();
static void unused_variable();
static void fpar_byvalue();
static void unused_struct_member();
//---------------------------------------------------------------------------
int main()
@ -61,6 +62,9 @@ int main()
unused_variable();
fpar_byvalue();
// unused struct member..
unused_struct_member();
std::cout << "Success Rate: "
<< SuccessCount
@ -925,3 +929,13 @@ static void fpar_byvalue()
}
//---------------------------------------------------------------------------
static void unused_struct_member()
{
check( CheckStructMemberUsage,
__LINE__,
"struct abc {int i;};",
"[test.cpp:1]: struct member 'abc::i' is never read\n" );
}