2008-11-06 01:00:38 +01:00
|
|
|
/*
|
2008-10-26 08:55:15 +01:00
|
|
|
* c++check - c/c++ syntax checking
|
|
|
|
* Copyright (C) 2007 Daniel Marjamäki
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
|
|
|
*/
|
|
|
|
|
2007-05-24 15:07:30 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#include "CheckClass.h"
|
2008-11-11 07:42:09 +01:00
|
|
|
|
2007-05-24 15:07:30 +02:00
|
|
|
#include <locale>
|
2008-11-11 07:42:09 +01:00
|
|
|
|
2007-05-24 15:07:30 +02:00
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
2008-08-30 20:29:37 +02:00
|
|
|
#include <cstring>
|
2008-10-21 10:47:26 +02:00
|
|
|
#include <algorithm>
|
2008-02-18 18:11:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
2008-09-11 19:03:58 +02:00
|
|
|
#include <ctype.h>
|
2008-02-18 18:11:34 +01:00
|
|
|
#include <mem.h>
|
|
|
|
#endif
|
2007-05-24 15:07:30 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-22 21:00:36 +01:00
|
|
|
CheckClass::CheckClass( const Tokenizer *tokenizer, const Settings &settings, ErrorLogger *errorLogger )
|
|
|
|
{
|
|
|
|
_tokenizer = tokenizer;
|
|
|
|
_settings = settings;
|
|
|
|
_errorLogger = errorLogger;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckClass::~CheckClass()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2008-11-11 07:42:09 +01:00
|
|
|
|
2007-05-24 15:07:30 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-12-06 18:00:14 +01:00
|
|
|
struct CheckClass::VAR *CheckClass::ClassChecking_GetVarList(const TOKEN *tok1)
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
|
|
|
// Get variable list..
|
|
|
|
struct VAR *varlist = NULL;
|
|
|
|
unsigned int indentlevel = 0;
|
2008-03-22 12:46:06 +01:00
|
|
|
for (const TOKEN *tok = tok1; tok; tok = tok->next)
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
|
|
|
if (!tok->next)
|
|
|
|
break;
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
if (tok->aaaa0() == '{')
|
2007-05-24 15:07:30 +02:00
|
|
|
indentlevel++;
|
2008-11-24 20:38:08 +01:00
|
|
|
if (tok->aaaa0() == '}')
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
|
|
|
if (indentlevel <= 1)
|
|
|
|
break;
|
|
|
|
indentlevel--;
|
|
|
|
}
|
|
|
|
|
2008-01-10 20:01:16 +01:00
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
if (indentlevel==1 && (strchr(";{}", tok->aaaa0()) || (tok->aaaa0()!=':' && strchr(tok->aaaa(), ':'))))
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
2008-03-22 12:46:06 +01:00
|
|
|
const TOKEN *next = tok->next;
|
2007-05-24 15:07:30 +02:00
|
|
|
|
2008-01-10 20:01:16 +01:00
|
|
|
const char *varname = 0;
|
2007-10-29 09:20:07 +01:00
|
|
|
|
2008-01-10 20:01:16 +01:00
|
|
|
// Is it a variable declaration?
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(next,"%type% %var% ;") )
|
2008-01-10 20:01:16 +01:00
|
|
|
{
|
|
|
|
const char *types[] = {"bool", "char", "int", "short", "long", "float", "double", 0};
|
|
|
|
for ( int type = 0; types[type]; type++ )
|
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( strcmp(next->aaaa(), types[type]) == 0)
|
2008-01-10 20:01:16 +01:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
varname = next->next->aaaa();
|
2008-01-10 20:01:16 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-10-23 08:37:13 +02:00
|
|
|
|
2008-01-10 20:01:16 +01:00
|
|
|
// Pointer?
|
2008-11-22 23:49:14 +01:00
|
|
|
else if ( TOKEN::Match(next, "%type% * %var% ;") )
|
2008-01-10 20:01:16 +01:00
|
|
|
{
|
2008-11-23 11:09:16 +01:00
|
|
|
varname = next->strAt(2);
|
2008-01-10 20:01:16 +01:00
|
|
|
}
|
2007-05-24 15:07:30 +02:00
|
|
|
|
2008-01-10 20:01:16 +01:00
|
|
|
if (varname)
|
|
|
|
{
|
|
|
|
struct VAR *var = new VAR;
|
|
|
|
memset(var, 0, sizeof(struct VAR));
|
|
|
|
var->name = varname;
|
2008-01-17 08:46:10 +01:00
|
|
|
var->init = false;
|
2008-01-10 20:01:16 +01:00
|
|
|
var->next = varlist;
|
|
|
|
varlist = var;
|
|
|
|
}
|
2007-05-24 15:07:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return varlist;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
2008-11-13 19:16:36 +01:00
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
const TOKEN * CheckClass::FindClassFunction( const TOKEN *tok, const char classname[], const char funcname[], int &indentlevel )
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
2008-03-28 18:40:24 +01:00
|
|
|
const char *_classname[2] = {0,0};
|
|
|
|
const char *_funcname[2] = {0,0};
|
|
|
|
_classname[0] = classname;
|
|
|
|
_funcname[0] = funcname;
|
|
|
|
|
2008-11-02 13:48:45 +01:00
|
|
|
if ( indentlevel < 0 || tok == NULL )
|
2008-11-01 19:01:35 +01:00
|
|
|
return NULL;
|
2008-03-28 18:40:24 +01:00
|
|
|
|
2008-11-01 19:01:35 +01:00
|
|
|
for ( ;tok; tok = tok->next )
|
2008-02-19 08:09:09 +01:00
|
|
|
{
|
2008-11-01 19:01:35 +01:00
|
|
|
if ( indentlevel == 0 &&
|
2008-11-22 23:49:14 +01:00
|
|
|
( TOKEN::Match(tok, "class %var1% {", _classname) ||
|
|
|
|
TOKEN::Match(tok, "class %var1% : %type% {", _classname) ) )
|
2008-02-19 08:09:09 +01:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok, "class %var% {") )
|
2008-11-23 11:09:16 +01:00
|
|
|
tok = tok->tokAt(3);
|
2008-11-01 19:01:35 +01:00
|
|
|
else
|
2008-11-23 11:09:16 +01:00
|
|
|
tok = tok->tokAt(5);
|
2008-11-01 19:01:35 +01:00
|
|
|
indentlevel = 1;
|
|
|
|
}
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok->aaaa0() == '{' )
|
2008-11-01 19:01:35 +01:00
|
|
|
{
|
|
|
|
// If indentlevel==0 don't go to indentlevel 1. Skip the block.
|
|
|
|
if ( indentlevel > 0 )
|
|
|
|
++indentlevel;
|
|
|
|
|
|
|
|
else
|
2008-02-19 08:09:09 +01:00
|
|
|
{
|
2008-11-01 19:01:35 +01:00
|
|
|
for ( ; tok; tok = tok->next )
|
2008-02-19 08:09:09 +01:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok->aaaa0() == '{' )
|
2008-11-01 19:01:35 +01:00
|
|
|
++indentlevel;
|
2008-11-24 20:38:08 +01:00
|
|
|
else if ( tok->aaaa0() == '}' )
|
2008-11-01 19:01:35 +01:00
|
|
|
{
|
|
|
|
--indentlevel;
|
|
|
|
if ( indentlevel <= 0 )
|
|
|
|
break;
|
|
|
|
}
|
2008-02-19 08:09:09 +01:00
|
|
|
}
|
2008-11-02 13:48:45 +01:00
|
|
|
if ( tok == NULL )
|
|
|
|
return NULL;
|
|
|
|
|
2008-11-01 19:01:35 +01:00
|
|
|
continue;
|
2008-02-19 08:09:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok->aaaa0() == '}' )
|
2008-02-19 08:09:09 +01:00
|
|
|
{
|
2008-11-01 19:01:35 +01:00
|
|
|
indentlevel--;
|
|
|
|
if ( indentlevel < 0 )
|
|
|
|
return NULL;
|
2008-02-19 08:09:09 +01:00
|
|
|
}
|
|
|
|
|
2008-11-01 19:01:35 +01:00
|
|
|
if ( indentlevel == 1 )
|
2008-02-19 08:09:09 +01:00
|
|
|
{
|
2008-11-01 19:01:35 +01:00
|
|
|
// Member function implemented in the class declaration?
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match( tok, "%var1% (", _funcname ) )
|
2008-11-01 19:01:35 +01:00
|
|
|
{
|
|
|
|
const TOKEN *tok2 = tok;
|
2008-11-24 20:38:08 +01:00
|
|
|
while ( tok2 && tok2->aaaa0() != '{' && tok2->aaaa0() != ';' )
|
2008-11-01 19:01:35 +01:00
|
|
|
tok2 = tok2->next;
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( tok2 && tok2->aaaa0() == '{' )
|
2008-11-01 19:01:35 +01:00
|
|
|
return tok;
|
|
|
|
}
|
2008-02-19 08:09:09 +01:00
|
|
|
}
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
else if ( indentlevel == 0 && TOKEN::Match(tok, "%var1% :: %var2% (", _classname, _funcname) )
|
2008-11-01 19:01:35 +01:00
|
|
|
{
|
|
|
|
return tok;
|
|
|
|
}
|
2008-02-19 08:09:09 +01:00
|
|
|
}
|
2007-05-24 15:07:30 +02:00
|
|
|
|
2008-02-19 08:09:09 +01:00
|
|
|
// Not found
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
2007-05-24 15:07:30 +02:00
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckClass::InitVar(struct VAR *varlist, const char varname[])
|
2008-03-23 15:15:44 +01:00
|
|
|
{
|
|
|
|
for (struct VAR *var = varlist; var; var = var->next)
|
|
|
|
{
|
|
|
|
if ( strcmp(var->name, varname) == 0 )
|
|
|
|
{
|
|
|
|
var->init = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckClass::ClassChecking_VarList_Initialize(const TOKEN *tok1, const TOKEN *ftok, struct VAR *varlist, const char classname[], std::list<std::string> &callstack)
|
2008-02-19 08:09:09 +01:00
|
|
|
{
|
2007-05-24 15:07:30 +02:00
|
|
|
bool Assign = false;
|
|
|
|
unsigned int indentlevel = 0;
|
2008-10-19 17:21:18 +02:00
|
|
|
|
2007-05-24 15:07:30 +02:00
|
|
|
for (; ftok; ftok = ftok->next)
|
|
|
|
{
|
|
|
|
if (!ftok->next)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Class constructor.. initializing variables like this
|
|
|
|
// clKalle::clKalle() : var(value) { }
|
2008-02-19 08:09:09 +01:00
|
|
|
if (indentlevel==0)
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
if (Assign && TOKEN::Match(ftok, "%var% ("))
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
InitVar( varlist, ftok->aaaa() );
|
2007-05-24 15:07:30 +02:00
|
|
|
}
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
Assign |= (ftok->aaaa0() == ':');
|
2007-05-24 15:07:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
if (ftok->aaaa0() == '{')
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
|
|
|
indentlevel++;
|
|
|
|
Assign = false;
|
|
|
|
}
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
if (ftok->aaaa0() == '}')
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
|
|
|
if (indentlevel <= 1)
|
|
|
|
break;
|
|
|
|
indentlevel--;
|
|
|
|
}
|
|
|
|
|
2008-11-01 07:41:45 +01:00
|
|
|
if ( indentlevel < 1 )
|
|
|
|
continue;
|
2008-03-23 15:15:44 +01:00
|
|
|
|
2008-11-13 19:16:36 +01:00
|
|
|
// Before a new statement there is "[{};)=]" or "else"
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( ! TOKEN::Match(ftok, "[{};)=]") && ! TOKEN::Match(ftok, "else") )
|
2008-11-01 07:41:45 +01:00
|
|
|
continue;
|
2007-05-24 15:07:30 +02:00
|
|
|
|
2008-11-01 18:00:36 +01:00
|
|
|
// Using the operator= function to initialize all variables..
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(ftok->next, "* this = ") )
|
2008-11-01 18:00:36 +01:00
|
|
|
{
|
|
|
|
for (struct VAR *var = varlist; var; var = var->next)
|
|
|
|
var->init = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if (!TOKEN::Match(ftok->next, "%var%") && !TOKEN::Match(ftok->next, "this . %var%"))
|
2008-11-01 07:41:45 +01:00
|
|
|
continue;
|
2007-05-24 15:07:30 +02:00
|
|
|
|
2008-11-01 07:41:45 +01:00
|
|
|
// Goto the first token in this statement..
|
|
|
|
ftok = ftok->next;
|
2007-05-24 15:07:30 +02:00
|
|
|
|
2008-11-01 07:41:45 +01:00
|
|
|
// Skip "this->"
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(ftok, "this .") )
|
2008-11-23 11:09:16 +01:00
|
|
|
ftok = ftok->tokAt(2);
|
2008-11-01 07:41:45 +01:00
|
|
|
|
|
|
|
// Clearing all variables..
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(ftok,"memset ( this ,"))
|
2008-11-01 07:41:45 +01:00
|
|
|
{
|
|
|
|
for (struct VAR *var = varlist; var; var = var->next)
|
|
|
|
var->init = true;
|
2008-11-01 18:00:36 +01:00
|
|
|
break;
|
2008-11-01 07:41:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calling member function?
|
2008-11-22 23:49:14 +01:00
|
|
|
else if (TOKEN::Match(ftok, "%var% ("))
|
2008-11-01 07:41:45 +01:00
|
|
|
{
|
|
|
|
// No recursive calls!
|
2008-11-24 20:38:08 +01:00
|
|
|
if ( std::find(callstack.begin(),callstack.end(),ftok->aaaa()) == callstack.end() )
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
callstack.push_back( ftok->aaaa() );
|
2008-11-01 19:01:35 +01:00
|
|
|
int i = 0;
|
2008-11-24 20:38:08 +01:00
|
|
|
const TOKEN *ftok2 = FindClassFunction( tok1, classname, ftok->aaaa(), i );
|
2008-11-01 19:01:35 +01:00
|
|
|
ClassChecking_VarList_Initialize(tok1, ftok2, varlist, classname, callstack);
|
2007-05-24 15:07:30 +02:00
|
|
|
}
|
|
|
|
}
|
2008-11-01 07:41:45 +01:00
|
|
|
|
|
|
|
// Assignment of member variable?
|
2008-11-22 23:49:14 +01:00
|
|
|
else if (TOKEN::Match(ftok, "%var% ="))
|
2008-11-01 07:41:45 +01:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
InitVar( varlist, ftok->aaaa() );
|
2008-11-01 07:41:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// The functions 'clear' and 'Clear' are supposed to initialize variable.
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(ftok,"%var% . clear (") || TOKEN::Match(ftok,"%var% . Clear ("))
|
2008-11-01 07:41:45 +01:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
InitVar( varlist, ftok->aaaa() );
|
2008-11-01 07:41:45 +01:00
|
|
|
}
|
2007-05-24 15:07:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// ClassCheck: Check that all class constructors are ok.
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckClass::CheckConstructors()
|
2008-12-06 20:14:04 +01:00
|
|
|
{
|
|
|
|
const char pattern_class[] = "class %var% {";
|
|
|
|
|
|
|
|
// Locate class
|
|
|
|
const TOKEN *tok1 = TOKEN::findmatch( _tokenizer->tokens(), pattern_class );
|
2007-05-24 15:07:30 +02:00
|
|
|
while (tok1)
|
2008-12-06 20:14:04 +01:00
|
|
|
{
|
|
|
|
const char *className[2];
|
|
|
|
className[0] = tok1->strAt( 1 );
|
|
|
|
className[1] = 0;
|
|
|
|
|
|
|
|
// TODO: handling of private constructors should be improved.
|
|
|
|
bool hasPrivateConstructor = false;
|
|
|
|
{
|
|
|
|
int indentlevel = 0;
|
|
|
|
bool isPrivate = true;
|
|
|
|
for ( const TOKEN *tok = tok1; tok; tok = tok->next )
|
|
|
|
{
|
|
|
|
// Indentation
|
|
|
|
if ( tok->str() == "{" )
|
|
|
|
++indentlevel;
|
|
|
|
|
|
|
|
else if ( tok->str() == "}" )
|
|
|
|
{
|
|
|
|
--indentlevel;
|
|
|
|
if (indentlevel <= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse class contents (indentlevel == 1)..
|
|
|
|
if ( indentlevel == 1 )
|
|
|
|
{
|
|
|
|
// What section are we in.. private/non-private
|
|
|
|
if ( tok->str() == "private:" )
|
|
|
|
isPrivate = true;
|
|
|
|
else if ( tok->str() == "protected:" || tok->str() == "public:" )
|
|
|
|
isPrivate = false;
|
|
|
|
|
|
|
|
// Is there a private constructor?
|
|
|
|
else if ( isPrivate && TOKEN::Match(tok, "%var1% (", className) )
|
|
|
|
{
|
|
|
|
hasPrivateConstructor = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( hasPrivateConstructor )
|
|
|
|
{
|
|
|
|
// TODO: Handle private constructors.
|
|
|
|
// Right now to avoid false positives I just bail out
|
|
|
|
tok1 = TOKEN::findmatch( tok1->next, pattern_class );
|
|
|
|
continue;
|
|
|
|
}
|
2007-05-24 15:07:30 +02:00
|
|
|
|
|
|
|
// Are there a class constructor?
|
2008-12-06 20:14:04 +01:00
|
|
|
const TOKEN *constructor_token = TOKEN::findmatch( tok1, "%any% %var1% (", className );
|
|
|
|
while ( TOKEN::Match( constructor_token, "~" ) )
|
|
|
|
constructor_token = TOKEN::findmatch( constructor_token->next, "%any% %var1% (", className );
|
|
|
|
|
|
|
|
// There are no constructor.
|
|
|
|
if ( ! constructor_token )
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
2008-12-06 20:14:04 +01:00
|
|
|
// If "--style" has been given, give a warning
|
2008-11-16 16:18:50 +01:00
|
|
|
if ( _settings._checkCodingStyle )
|
2008-02-18 18:04:14 +01:00
|
|
|
{
|
2008-12-06 20:14:04 +01:00
|
|
|
// If the class has member variables there should be an constructor
|
2008-11-01 19:01:35 +01:00
|
|
|
struct VAR *varlist = ClassChecking_GetVarList(tok1);
|
2008-02-22 08:26:10 +01:00
|
|
|
if ( varlist )
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2008-11-20 21:54:52 +01:00
|
|
|
ostr << _tokenizer->fileLine(tok1);
|
2008-12-06 20:14:04 +01:00
|
|
|
ostr << " The class '" << className[0] << "' has no constructor";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(ostr.str());
|
2008-02-22 08:26:10 +01:00
|
|
|
}
|
|
|
|
// Delete the varlist..
|
|
|
|
while (varlist)
|
|
|
|
{
|
|
|
|
struct VAR *nextvar = varlist->next;
|
|
|
|
delete varlist;
|
|
|
|
varlist = nextvar;
|
|
|
|
}
|
2008-02-18 18:04:14 +01:00
|
|
|
}
|
|
|
|
|
2008-12-06 20:14:04 +01:00
|
|
|
tok1 = TOKEN::findmatch( tok1->next, pattern_class );
|
2007-05-24 15:07:30 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that all member variables are initialized..
|
2008-11-01 19:01:35 +01:00
|
|
|
struct VAR *varlist = ClassChecking_GetVarList(tok1);
|
2007-05-24 15:07:30 +02:00
|
|
|
|
2008-11-01 19:01:35 +01:00
|
|
|
int indentlevel = 0;
|
2008-12-06 20:14:04 +01:00
|
|
|
constructor_token = FindClassFunction( tok1, className[0], className[0], indentlevel );
|
2008-10-19 17:21:18 +02:00
|
|
|
std::list<std::string> callstack;
|
2008-12-06 20:14:04 +01:00
|
|
|
ClassChecking_VarList_Initialize(tok1, constructor_token, varlist, className[0], callstack);
|
2008-01-17 08:46:10 +01:00
|
|
|
while ( constructor_token )
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
2008-01-17 08:46:10 +01:00
|
|
|
// Check if any variables are uninitialized
|
|
|
|
for (struct VAR *var = varlist; var; var = var->next)
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
2008-12-06 20:14:04 +01:00
|
|
|
// Is it a static member variable?
|
|
|
|
std::ostringstream pattern;
|
|
|
|
pattern << className[0] << "::" << var->name << "=";
|
|
|
|
if (TOKEN::findmatch(_tokenizer->tokens(), pattern.str().c_str()))
|
2008-01-17 08:46:10 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!var->init)
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2008-11-20 21:54:52 +01:00
|
|
|
ostr << _tokenizer->fileLine(constructor_token);
|
2008-12-06 20:14:04 +01:00
|
|
|
ostr << " Uninitialized member variable '" << className[0] << "::" << var->name << "'";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(ostr.str());
|
2008-01-17 08:46:10 +01:00
|
|
|
}
|
2007-05-24 15:07:30 +02:00
|
|
|
}
|
2008-01-17 08:46:10 +01:00
|
|
|
|
|
|
|
for ( struct VAR *var = varlist; var; var = var->next )
|
|
|
|
var->init = false;
|
2008-02-19 08:09:09 +01:00
|
|
|
|
2008-12-06 20:14:04 +01:00
|
|
|
constructor_token = FindClassFunction( constructor_token->next, className[0], className[0], indentlevel );
|
2008-11-15 16:22:10 +01:00
|
|
|
callstack.clear();
|
2008-12-06 20:14:04 +01:00
|
|
|
ClassChecking_VarList_Initialize(tok1, constructor_token, varlist, className[0], callstack);
|
2007-05-24 15:07:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the varlist..
|
|
|
|
while (varlist)
|
|
|
|
{
|
|
|
|
struct VAR *nextvar = varlist->next;
|
|
|
|
delete varlist;
|
|
|
|
varlist = nextvar;
|
|
|
|
}
|
|
|
|
|
2008-12-06 20:14:04 +01:00
|
|
|
tok1 = TOKEN::findmatch( tok1->next, pattern_class );
|
2007-05-24 15:07:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// ClassCheck: Unused private functions
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckClass::CheckUnusedPrivateFunctions()
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
|
|
|
// Locate some class
|
|
|
|
const char *pattern_class[] = {"class","","{",NULL};
|
2008-11-22 23:49:14 +01:00
|
|
|
for (const TOKEN *tok1 = TOKEN::findtoken(_tokenizer->tokens(), pattern_class); tok1; tok1 = TOKEN::findtoken(tok1->next, pattern_class))
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
const char *classname = tok1->next->aaaa();
|
2007-05-24 15:07:30 +02:00
|
|
|
|
|
|
|
// The class implementation must be available..
|
|
|
|
const char *pattern_classconstructor[] = {"","::","",NULL};
|
|
|
|
pattern_classconstructor[0] = classname;
|
|
|
|
pattern_classconstructor[2] = classname;
|
2008-11-22 23:49:14 +01:00
|
|
|
if (!TOKEN::findtoken(_tokenizer->tokens(),pattern_classconstructor))
|
2007-05-24 15:07:30 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Get private functions..
|
|
|
|
std::list<std::string> FuncList;
|
|
|
|
FuncList.clear();
|
|
|
|
bool priv = false;
|
|
|
|
unsigned int indent_level = 0;
|
2008-03-22 12:46:06 +01:00
|
|
|
for (const TOKEN *tok = tok1; tok; tok = tok->next)
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(tok,"friend %var%"))
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
|
|
|
// Todo: Handle friend classes
|
|
|
|
FuncList.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
if (tok->aaaa0() == '{')
|
2007-05-24 15:07:30 +02:00
|
|
|
indent_level++;
|
2008-11-24 20:38:08 +01:00
|
|
|
if (tok->aaaa0() == '}')
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
|
|
|
if (indent_level <= 1)
|
|
|
|
break;
|
|
|
|
indent_level--;
|
|
|
|
}
|
2008-11-24 20:38:08 +01:00
|
|
|
if (strcmp(tok->aaaa(),"};") == 0)
|
2007-05-24 15:07:30 +02:00
|
|
|
break;
|
2008-11-24 20:38:08 +01:00
|
|
|
if (strcmp(tok->aaaa(),"private:") == 0)
|
2007-05-24 15:07:30 +02:00
|
|
|
priv = true;
|
2008-11-24 20:38:08 +01:00
|
|
|
else if (strcmp(tok->aaaa(),"public:") == 0)
|
2007-05-24 15:07:30 +02:00
|
|
|
priv = false;
|
2008-11-24 20:38:08 +01:00
|
|
|
else if (strcmp(tok->aaaa(),"protected:") == 0)
|
2007-05-24 15:07:30 +02:00
|
|
|
priv = false;
|
|
|
|
else if (priv && indent_level == 1)
|
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
if ( TOKEN::Match(tok, "typedef %type% (") )
|
2008-11-23 11:09:16 +01:00
|
|
|
tok = tok->tokAt(2);
|
2008-11-05 08:25:28 +01:00
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(tok, "%var% (") &&
|
|
|
|
!TOKEN::Match(tok,classname))
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
FuncList.push_back(tok->aaaa());
|
2007-05-24 15:07:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that all private functions are used..
|
|
|
|
const char *pattern_function[] = {"","::",NULL};
|
|
|
|
pattern_function[0] = classname;
|
|
|
|
bool HasFuncImpl = false;
|
2008-11-16 16:58:52 +01:00
|
|
|
const TOKEN *ftok = _tokenizer->tokens();
|
2008-11-06 01:00:38 +01:00
|
|
|
while (ftok)
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
ftok = TOKEN::findtoken(ftok,pattern_function);
|
2007-05-24 15:07:30 +02:00
|
|
|
int numpar = 0;
|
2008-11-24 20:38:08 +01:00
|
|
|
while (ftok && ftok->aaaa0()!=';' && ftok->aaaa0()!='{')
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if (ftok->aaaa0() == '(')
|
2007-05-24 15:07:30 +02:00
|
|
|
numpar++;
|
2008-11-24 20:38:08 +01:00
|
|
|
else if (ftok->aaaa0() == ')')
|
2007-05-24 15:07:30 +02:00
|
|
|
numpar--;
|
|
|
|
ftok = ftok->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ftok)
|
|
|
|
break;
|
|
|
|
|
2008-11-24 20:38:08 +01:00
|
|
|
if (ftok->aaaa0() != ';' && numpar == 0)
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
2008-11-06 01:00:38 +01:00
|
|
|
HasFuncImpl = true;
|
|
|
|
|
|
|
|
indent_level = 0;
|
|
|
|
while (ftok)
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if (ftok->aaaa0() == '{')
|
2008-11-06 01:00:38 +01:00
|
|
|
indent_level++;
|
2008-11-24 20:38:08 +01:00
|
|
|
if (ftok->aaaa0() == '}')
|
2008-11-06 01:00:38 +01:00
|
|
|
{
|
|
|
|
if (indent_level<=1)
|
|
|
|
break;
|
|
|
|
indent_level--;
|
|
|
|
}
|
2008-11-24 20:38:08 +01:00
|
|
|
if (ftok->next && ftok->next->aaaa0() == '(')
|
|
|
|
FuncList.remove(ftok->aaaa());
|
2008-11-06 01:00:38 +01:00
|
|
|
ftok = ftok->next;
|
2007-05-24 15:07:30 +02:00
|
|
|
}
|
|
|
|
}
|
2008-11-06 01:00:38 +01:00
|
|
|
|
|
|
|
if (ftok)
|
|
|
|
ftok = ftok->next;
|
2007-05-24 15:07:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (HasFuncImpl && !FuncList.empty())
|
|
|
|
{
|
|
|
|
bool fp = false;
|
|
|
|
|
|
|
|
// Final check; check if the function pointer is used somewhere..
|
|
|
|
const char *_pattern[] = {"=","",NULL};
|
|
|
|
_pattern[1] = FuncList.front().c_str();
|
2008-11-22 23:49:14 +01:00
|
|
|
fp |= (TOKEN::findtoken(_tokenizer->tokens(), _pattern) != NULL);
|
2008-11-11 21:19:28 +01:00
|
|
|
_pattern[0] = "return";
|
2008-11-22 23:49:14 +01:00
|
|
|
fp |= (TOKEN::findtoken(_tokenizer->tokens(), _pattern) != NULL);
|
2007-05-24 15:07:30 +02:00
|
|
|
_pattern[0] = "(";
|
2008-11-22 23:49:14 +01:00
|
|
|
fp |= (TOKEN::findtoken(_tokenizer->tokens(), _pattern) != NULL);
|
2007-05-24 15:07:30 +02:00
|
|
|
_pattern[0] = ")";
|
2008-11-22 23:49:14 +01:00
|
|
|
fp |= (TOKEN::findtoken(_tokenizer->tokens(), _pattern) != NULL);
|
2007-05-24 15:07:30 +02:00
|
|
|
_pattern[0] = ",";
|
2008-11-22 23:49:14 +01:00
|
|
|
fp |= (TOKEN::findtoken(_tokenizer->tokens(), _pattern) != NULL);
|
2007-05-24 15:07:30 +02:00
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << "Class '" << classname << "', unused private function: '" << FuncList.front() << "'";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(ostr.str());
|
2007-05-24 15:07:30 +02:00
|
|
|
}
|
|
|
|
FuncList.pop_front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// ClassCheck: Check that memset is not used on classes
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckClass::CheckMemset()
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
|
|
|
// Locate all 'memset' tokens..
|
2008-11-16 16:58:52 +01:00
|
|
|
for (const TOKEN *tok = _tokenizer->tokens(); tok; tok = tok->next)
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
2008-11-22 23:49:14 +01:00
|
|
|
if (!TOKEN::Match(tok,"memset") && !TOKEN::Match(tok,"memcpy") && !TOKEN::Match(tok,"memmove"))
|
2007-05-24 15:07:30 +02:00
|
|
|
continue;
|
|
|
|
|
2008-04-12 14:07:35 +02:00
|
|
|
// Todo: Handle memcpy and memmove
|
2007-05-24 15:07:30 +02:00
|
|
|
const char *type = NULL;
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(tok, "memset ( %var% , %num% , sizeof ( %type% ) )"))
|
2008-11-23 11:09:16 +01:00
|
|
|
type = tok->strAt(8);
|
2008-11-22 23:49:14 +01:00
|
|
|
else if (TOKEN::Match(tok, "memset ( & %var% , %num% , sizeof ( %type% ) )"))
|
2008-11-23 11:09:16 +01:00
|
|
|
type = tok->strAt(9);
|
2008-11-22 23:49:14 +01:00
|
|
|
else if (TOKEN::Match(tok, "memset ( %var% , %num% , sizeof ( struct %type% ) )"))
|
2008-11-23 11:09:16 +01:00
|
|
|
type = tok->strAt(9);
|
2008-11-22 23:49:14 +01:00
|
|
|
else if (TOKEN::Match(tok, "memset ( & %var% , %num% , sizeof ( struct %type% ) )"))
|
2008-11-23 11:09:16 +01:00
|
|
|
type = tok->strAt(10);
|
2008-11-22 23:49:14 +01:00
|
|
|
else if (TOKEN::Match(tok, "%type% ( %var% , %var% , sizeof ( %type% ) )"))
|
2008-11-23 11:09:16 +01:00
|
|
|
type = tok->strAt(8);
|
2007-05-24 15:07:30 +02:00
|
|
|
|
|
|
|
// No type defined => The tokens didn't match
|
|
|
|
if (!(type && type[0]))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Warn if type is a class..
|
|
|
|
const char *pattern1[] = {"class","",NULL};
|
|
|
|
pattern1[1] = type;
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::findtoken(_tokenizer->tokens(),pattern1))
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2008-11-24 20:38:08 +01:00
|
|
|
ostr << _tokenizer->fileLine(tok) << ": Using '" << tok->aaaa() << "' on class.";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(ostr.str());
|
2007-07-20 08:18:33 +02:00
|
|
|
continue;
|
2007-05-24 15:07:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Warn if type is a struct that contains any std::*
|
|
|
|
const char *pattern2[] = {"struct","","{",NULL};
|
|
|
|
pattern2[1] = type;
|
2008-11-22 23:49:14 +01:00
|
|
|
for (const TOKEN *tstruct = TOKEN::findtoken(_tokenizer->tokens(), pattern2); tstruct; tstruct = tstruct->next)
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
2008-11-24 20:38:08 +01:00
|
|
|
if (tstruct->aaaa0() == '}')
|
2007-05-24 15:07:30 +02:00
|
|
|
break;
|
|
|
|
|
2008-11-22 23:49:14 +01:00
|
|
|
if (TOKEN::Match(tstruct, "std :: %type% %var% ;"))
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2008-11-24 20:38:08 +01:00
|
|
|
ostr << _tokenizer->fileLine(tok) << ": Using '" << tok->aaaa() << "' on struct that contains a 'std::" << tstruct->strAt(2) << "'";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(ostr.str());
|
2007-05-24 15:07:30 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-06 18:34:34 +01:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
2007-05-24 15:07:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// ClassCheck: "void operator=("
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
void CheckClass::CheckOperatorEq1()
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
|
|
|
const char *pattern[] = {"void", "operator", "=", "(", NULL};
|
2008-11-22 23:49:14 +01:00
|
|
|
if (const TOKEN *tok = TOKEN::findtoken(_tokenizer->tokens(),pattern))
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2008-11-20 21:54:52 +01:00
|
|
|
ostr << _tokenizer->fileLine(tok) << ": 'operator=' should return something";
|
2008-11-20 23:19:26 +01:00
|
|
|
_errorLogger->reportErr(ostr.str());
|
2007-05-24 15:07:30 +02:00
|
|
|
}
|
2008-12-06 18:34:34 +01:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// A destructor in a base class should be virtual
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void CheckClass::virtualDestructor()
|
|
|
|
{
|
|
|
|
const TOKEN *derived = TOKEN::findmatch(_tokenizer->tokens(), "class %var% : public %var%");
|
|
|
|
while (derived)
|
|
|
|
{
|
2008-12-06 20:26:13 +01:00
|
|
|
// Iterate through each base class...
|
|
|
|
for ( derived = derived->tokAt(3); TOKEN::Match(derived, "public %var%"); derived = derived->tokAt(3) )
|
|
|
|
{
|
|
|
|
// Name of base class..
|
|
|
|
const char *baseName[2];
|
|
|
|
baseName[0] = derived->strAt(1);
|
|
|
|
baseName[1] = 0;
|
2008-12-06 18:34:34 +01:00
|
|
|
|
2008-12-06 20:26:13 +01:00
|
|
|
// Find the destructor for the base class.
|
|
|
|
const TOKEN *base = TOKEN::findmatch(_tokenizer->tokens(), "%any% ~ %var1% (", baseName);
|
|
|
|
while (TOKEN::Match(base, "::"))
|
|
|
|
base = TOKEN::findmatch(base->next, "%any% ~ %var1% (", baseName);
|
2008-12-06 18:34:34 +01:00
|
|
|
|
2008-12-06 20:26:13 +01:00
|
|
|
// Check that there is a destructor..
|
|
|
|
if ( ! base )
|
|
|
|
{
|
|
|
|
// Is the class declaration available?
|
|
|
|
base = TOKEN::findmatch(_tokenizer->tokens(), "class %var1% :|{", baseName);
|
|
|
|
if ( base )
|
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
|
|
|
errmsg << _tokenizer->fileLine(base) << ": Base class " << baseName[0] << " doesn't have a virtual destructor";
|
|
|
|
_errorLogger->reportErr(errmsg.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// There is a destructor. Check that it's virtual..
|
|
|
|
else if ( ! TOKEN::Match(base, "virtual") )
|
2008-12-06 18:34:34 +01:00
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
2008-12-06 20:26:13 +01:00
|
|
|
errmsg << _tokenizer->fileLine(base) << ": The destructor for the base class " << baseName[0] << " is not virtual";
|
2008-12-06 18:34:34 +01:00
|
|
|
_errorLogger->reportErr(errmsg.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-06 20:26:13 +01:00
|
|
|
derived = TOKEN::findmatch(derived, "class %var% : public %var%");
|
2008-12-06 18:34:34 +01:00
|
|
|
}
|
2007-05-24 15:07:30 +02:00
|
|
|
}
|
2008-12-06 18:34:34 +01:00
|
|
|
//---------------------------------------------------------------------------
|
2007-05-24 15:07:30 +02:00
|
|
|
|