CheckStructMemberUsage: Check for unused struct members
This commit is contained in:
parent
7464adfb47
commit
219d82e180
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -43,6 +43,9 @@ void CheckVariableScope();
|
|||
// Check for constant function parameter
|
||||
void CheckConstantFunctionParameter();
|
||||
|
||||
// Check that all struct members are used
|
||||
void CheckStructMemberUsage();
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
#endif
|
||||
|
||||
|
|
5
main.cpp
5
main.cpp
|
@ -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..
|
||||
|
|
14
tests.cpp
14
tests.cpp
|
@ -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" );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue