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 "CommonCheck.h"
|
|
|
|
#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-02-18 18:04:14 +01:00
|
|
|
extern bool CheckCodingStyle;
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
|
2007-05-24 15:07:30 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-11-11 07:42:09 +01:00
|
|
|
struct 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;
|
|
|
|
|
|
|
|
if (tok->str[0] == '{')
|
|
|
|
indentlevel++;
|
|
|
|
if (tok->str[0] == '}')
|
|
|
|
{
|
|
|
|
if (indentlevel <= 1)
|
|
|
|
break;
|
|
|
|
indentlevel--;
|
|
|
|
}
|
|
|
|
|
2008-01-10 20:01:16 +01:00
|
|
|
|
|
|
|
if (indentlevel==1 && (strchr(";{}", tok->str[0]) || (tok->str[0]!=':' && strchr(tok->str, ':'))))
|
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-03-28 08:18:03 +01:00
|
|
|
if ( 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++ )
|
|
|
|
{
|
|
|
|
if ( strcmp(next->str, types[type]) == 0)
|
|
|
|
{
|
|
|
|
varname = next->next->str;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-10-23 08:37:13 +02:00
|
|
|
|
2008-01-10 20:01:16 +01:00
|
|
|
// Pointer?
|
2008-03-28 08:18:03 +01:00
|
|
|
else if ( Match(next, "%type% * %var% ;") )
|
2008-01-10 20:01:16 +01:00
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
varname = Tokenizer::getstr(next, 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
|
|
|
|
|
|
|
CheckClass::CheckClass( Tokenizer *tokenizer )
|
|
|
|
{
|
|
|
|
_tokenizer = tokenizer;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckClass::~CheckClass()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2007-05-24 15:07:30 +02: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 &&
|
|
|
|
( Match(tok, "class %var1% {", _classname) ||
|
|
|
|
Match(tok, "class %var1% : %type% {", _classname) ) )
|
2008-02-19 08:09:09 +01:00
|
|
|
{
|
2008-11-01 19:01:35 +01:00
|
|
|
if ( Match(tok, "class %var% {") )
|
2008-11-09 08:19:53 +01:00
|
|
|
tok = Tokenizer::gettok(tok, 3);
|
2008-11-01 19:01:35 +01:00
|
|
|
else
|
2008-11-09 08:19:53 +01:00
|
|
|
tok = Tokenizer::gettok(tok, 5);
|
2008-11-01 19:01:35 +01:00
|
|
|
indentlevel = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( tok->str[0] == '{' )
|
|
|
|
{
|
|
|
|
// 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-01 19:01:35 +01:00
|
|
|
if ( tok->str[0] == '{' )
|
|
|
|
++indentlevel;
|
|
|
|
else if ( tok->str[0] == '}' )
|
|
|
|
{
|
|
|
|
--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-01 19:01:35 +01:00
|
|
|
if ( tok->str[0] == '}' )
|
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?
|
|
|
|
if ( Match( tok, "%var1% (", _funcname ) )
|
|
|
|
{
|
|
|
|
const TOKEN *tok2 = tok;
|
|
|
|
while ( tok2 && tok2->str[0] != '{' && tok2->str[0] != ';' )
|
|
|
|
tok2 = tok2->next;
|
|
|
|
if ( tok2 && tok2->str[0] == '{' )
|
|
|
|
return tok;
|
|
|
|
}
|
2008-02-19 08:09:09 +01:00
|
|
|
}
|
|
|
|
|
2008-11-01 19:01:35 +01:00
|
|
|
else if ( indentlevel == 0 && Match(tok, "%var1% :: %var2% (", _classname, _funcname) )
|
|
|
|
{
|
|
|
|
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-03-28 08:18:03 +01:00
|
|
|
if (Assign && Match(ftok, "%var% ("))
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
2008-03-23 15:15:44 +01:00
|
|
|
InitVar( varlist, ftok->str );
|
2007-05-24 15:07:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Assign |= (ftok->str[0] == ':');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (ftok->str[0] == '{')
|
|
|
|
{
|
|
|
|
indentlevel++;
|
|
|
|
Assign = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ftok->str[0] == '}')
|
|
|
|
{
|
|
|
|
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"
|
|
|
|
if ( ! Match(ftok, "[{};)=]") && ! 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..
|
|
|
|
if ( Match(ftok->next, "* this = ") )
|
|
|
|
{
|
|
|
|
for (struct VAR *var = varlist; var; var = var->next)
|
|
|
|
var->init = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-11-01 07:41:45 +01:00
|
|
|
if (!Match(ftok->next, "%var%") && !Match(ftok->next, "this . %var%"))
|
|
|
|
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->"
|
|
|
|
if ( Match(ftok, "this .") )
|
2008-11-09 08:19:53 +01:00
|
|
|
ftok = Tokenizer::gettok(ftok, 2);
|
2008-11-01 07:41:45 +01:00
|
|
|
|
|
|
|
// Clearing all variables..
|
|
|
|
if (Match(ftok,"memset ( this ,"))
|
|
|
|
{
|
|
|
|
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?
|
|
|
|
else if (Match(ftok, "%var% ("))
|
|
|
|
{
|
|
|
|
// No recursive calls!
|
|
|
|
if ( std::find(callstack.begin(),callstack.end(),ftok->str) == callstack.end() )
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
2008-11-01 07:41:45 +01:00
|
|
|
callstack.push_back( ftok->str );
|
2008-11-01 19:01:35 +01:00
|
|
|
int i = 0;
|
|
|
|
const TOKEN *ftok2 = FindClassFunction( tok1, classname, ftok->str, i );
|
|
|
|
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?
|
|
|
|
else if (Match(ftok, "%var% ="))
|
|
|
|
{
|
|
|
|
InitVar( varlist, ftok->str );
|
|
|
|
}
|
|
|
|
|
|
|
|
// The functions 'clear' and 'Clear' are supposed to initialize variable.
|
|
|
|
if (Match(ftok,"%var% . clear (") || Match(ftok,"%var% . Clear ("))
|
|
|
|
{
|
|
|
|
InitVar( varlist, ftok->str );
|
|
|
|
}
|
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()
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
|
|
|
// Locate class
|
|
|
|
const char *pattern_classname[] = {"class","","{",NULL};
|
2008-11-09 08:19:53 +01:00
|
|
|
const TOKEN *tok1 = Tokenizer::findtoken(tokens, pattern_classname);
|
2007-05-24 15:07:30 +02:00
|
|
|
while (tok1)
|
|
|
|
{
|
|
|
|
const char *classname = tok1->next->str;
|
2008-03-18 08:46:42 +01:00
|
|
|
if ( ! IsName(classname) )
|
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
tok1 = Tokenizer::findtoken( tok1->next, pattern_classname );
|
2008-03-18 08:46:42 +01:00
|
|
|
continue;
|
|
|
|
}
|
2007-05-24 15:07:30 +02:00
|
|
|
|
|
|
|
// Are there a class constructor?
|
2008-02-18 17:28:37 +01:00
|
|
|
const char *constructor_pattern[] = {"","clKalle","(",NULL};
|
|
|
|
constructor_pattern[1] = classname;
|
2008-11-09 08:19:53 +01:00
|
|
|
const TOKEN *constructor_token = Tokenizer::findtoken( tokens, constructor_pattern );
|
2008-02-18 17:28:37 +01:00
|
|
|
while ( constructor_token && constructor_token->str[0] == '~' )
|
2008-11-09 08:19:53 +01:00
|
|
|
constructor_token = Tokenizer::findtoken( constructor_token->next, constructor_pattern );
|
2008-02-18 17:28:37 +01:00
|
|
|
if ( ! constructor_token )
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
2008-02-18 17:28:37 +01:00
|
|
|
// There's no class constructor
|
2008-02-18 18:04:14 +01:00
|
|
|
if ( CheckCodingStyle )
|
|
|
|
{
|
2008-02-22 08:26:10 +01:00
|
|
|
// Check that all member variables are initialized..
|
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-13 23:39:47 +01:00
|
|
|
ostr << FileLine(tok1, _tokenizer);
|
2008-02-22 08:26:10 +01:00
|
|
|
ostr << " The class '" << classname << "' has no constructor";
|
|
|
|
ReportErr(ostr.str());
|
|
|
|
}
|
|
|
|
// Delete the varlist..
|
|
|
|
while (varlist)
|
|
|
|
{
|
|
|
|
struct VAR *nextvar = varlist->next;
|
|
|
|
delete varlist;
|
|
|
|
varlist = nextvar;
|
|
|
|
}
|
2008-02-18 18:04:14 +01:00
|
|
|
}
|
|
|
|
|
2008-11-09 08:19:53 +01:00
|
|
|
tok1 = Tokenizer::findtoken( tok1->next, pattern_classname );
|
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;
|
|
|
|
constructor_token = FindClassFunction( tok1, classname, classname, indentlevel );
|
2008-10-19 17:21:18 +02:00
|
|
|
std::list<std::string> callstack;
|
2008-11-01 19:01:35 +01:00
|
|
|
ClassChecking_VarList_Initialize(tok1, constructor_token, varlist, classname, 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-01-17 08:46:10 +01:00
|
|
|
// Is it a static member variable?
|
|
|
|
const char *pattern[] = {"","::","","=",NULL};
|
|
|
|
pattern[0] = classname;
|
|
|
|
pattern[2] = var->name;
|
2008-11-09 08:19:53 +01:00
|
|
|
if (Tokenizer::findtoken(tokens, pattern))
|
2008-01-17 08:46:10 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!var->init)
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2008-11-13 23:39:47 +01:00
|
|
|
ostr << FileLine(constructor_token, _tokenizer);
|
2008-01-17 08:46:10 +01:00
|
|
|
ostr << " Uninitialized member variable '" << classname << "::" << var->name << "'";
|
|
|
|
ReportErr(ostr.str());
|
|
|
|
}
|
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
|
|
|
|
|
|
|
constructor_token = FindClassFunction( constructor_token->next, classname, classname, indentlevel );
|
2008-11-15 16:22:10 +01:00
|
|
|
callstack.clear();
|
2008-11-01 19:01:35 +01:00
|
|
|
ClassChecking_VarList_Initialize(tok1, constructor_token, varlist, classname, callstack);
|
2007-05-24 15:07:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the varlist..
|
|
|
|
while (varlist)
|
|
|
|
{
|
|
|
|
struct VAR *nextvar = varlist->next;
|
|
|
|
delete varlist;
|
|
|
|
varlist = nextvar;
|
|
|
|
}
|
|
|
|
|
2008-11-09 08:19:53 +01:00
|
|
|
tok1 = Tokenizer::findtoken( tok1->next, pattern_classname );
|
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-09 08:19:53 +01:00
|
|
|
for (const TOKEN *tok1 = Tokenizer::findtoken(tokens, pattern_class); tok1; tok1 = Tokenizer::findtoken(tok1->next, pattern_class))
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
|
|
|
const char *classname = tok1->next->str;
|
|
|
|
|
|
|
|
// The class implementation must be available..
|
|
|
|
const char *pattern_classconstructor[] = {"","::","",NULL};
|
|
|
|
pattern_classconstructor[0] = classname;
|
|
|
|
pattern_classconstructor[2] = classname;
|
2008-11-09 08:19:53 +01:00
|
|
|
if (!Tokenizer::findtoken(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-03-28 08:18:03 +01:00
|
|
|
if (Match(tok,"friend %var%"))
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
|
|
|
// Todo: Handle friend classes
|
|
|
|
FuncList.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok->str[0] == '{')
|
|
|
|
indent_level++;
|
|
|
|
if (tok->str[0] == '}')
|
|
|
|
{
|
|
|
|
if (indent_level <= 1)
|
|
|
|
break;
|
|
|
|
indent_level--;
|
|
|
|
}
|
|
|
|
if (strcmp(tok->str,"};") == 0)
|
|
|
|
break;
|
|
|
|
if (strcmp(tok->str,"private:") == 0)
|
|
|
|
priv = true;
|
|
|
|
else if (strcmp(tok->str,"public:") == 0)
|
|
|
|
priv = false;
|
|
|
|
else if (strcmp(tok->str,"protected:") == 0)
|
|
|
|
priv = false;
|
|
|
|
else if (priv && indent_level == 1)
|
|
|
|
{
|
2008-11-05 08:25:28 +01:00
|
|
|
if ( Match(tok, "typedef %type% (") )
|
2008-11-09 08:19:53 +01:00
|
|
|
tok = Tokenizer::gettok(tok, 2);
|
2008-11-05 08:25:28 +01:00
|
|
|
|
|
|
|
if (Match(tok, "%var% (") &&
|
|
|
|
!Match(tok,classname))
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
|
|
|
FuncList.push_back(tok->str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that all private functions are used..
|
|
|
|
const char *pattern_function[] = {"","::",NULL};
|
|
|
|
pattern_function[0] = classname;
|
|
|
|
bool HasFuncImpl = false;
|
2008-11-06 01:00:38 +01:00
|
|
|
const TOKEN *ftok = tokens;
|
|
|
|
while (ftok)
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
2008-11-09 08:19:53 +01:00
|
|
|
ftok = Tokenizer::findtoken(ftok,pattern_function);
|
2007-05-24 15:07:30 +02:00
|
|
|
int numpar = 0;
|
|
|
|
while (ftok && ftok->str[0]!=';' && ftok->str[0]!='{')
|
|
|
|
{
|
|
|
|
if (ftok->str[0] == '(')
|
|
|
|
numpar++;
|
|
|
|
else if (ftok->str[0] == ')')
|
|
|
|
numpar--;
|
|
|
|
ftok = ftok->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ftok)
|
|
|
|
break;
|
|
|
|
|
2008-11-06 01:00:38 +01:00
|
|
|
if (ftok->str[0] != ';' && 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-06 01:00:38 +01:00
|
|
|
if (ftok->str[0] == '{')
|
|
|
|
indent_level++;
|
|
|
|
if (ftok->str[0] == '}')
|
|
|
|
{
|
|
|
|
if (indent_level<=1)
|
|
|
|
break;
|
|
|
|
indent_level--;
|
|
|
|
}
|
|
|
|
if (ftok->next && ftok->next->str[0] == '(')
|
|
|
|
FuncList.remove(ftok->str);
|
|
|
|
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-09 08:19:53 +01:00
|
|
|
fp |= (Tokenizer::findtoken(tokens, _pattern) != NULL);
|
2008-11-11 21:19:28 +01:00
|
|
|
_pattern[0] = "return";
|
|
|
|
fp |= (Tokenizer::findtoken(tokens, _pattern) != NULL);
|
2007-05-24 15:07:30 +02:00
|
|
|
_pattern[0] = "(";
|
2008-11-09 08:19:53 +01:00
|
|
|
fp |= (Tokenizer::findtoken(tokens, _pattern) != NULL);
|
2007-05-24 15:07:30 +02:00
|
|
|
_pattern[0] = ")";
|
2008-11-09 08:19:53 +01:00
|
|
|
fp |= (Tokenizer::findtoken(tokens, _pattern) != NULL);
|
2007-05-24 15:07:30 +02:00
|
|
|
_pattern[0] = ",";
|
2008-11-09 08:19:53 +01:00
|
|
|
fp |= (Tokenizer::findtoken(tokens, _pattern) != NULL);
|
2007-05-24 15:07:30 +02:00
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << "Class '" << classname << "', unused private function: '" << FuncList.front() << "'";
|
|
|
|
ReportErr(ostr.str());
|
|
|
|
}
|
|
|
|
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-03-22 12:46:06 +01:00
|
|
|
for (const TOKEN *tok = tokens; tok; tok = tok->next)
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
2008-04-12 14:07:35 +02:00
|
|
|
if (!Match(tok,"memset") && !Match(tok,"memcpy") && !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-03-28 08:18:03 +01:00
|
|
|
if (Match(tok, "memset ( %var% , %num% , sizeof ( %type% ) )"))
|
2008-11-09 08:19:53 +01:00
|
|
|
type = Tokenizer::getstr(tok, 8);
|
2008-03-28 08:18:03 +01:00
|
|
|
else if (Match(tok, "memset ( & %var% , %num% , sizeof ( %type% ) )"))
|
2008-11-09 08:19:53 +01:00
|
|
|
type = Tokenizer::getstr(tok, 9);
|
2008-03-28 08:18:03 +01:00
|
|
|
else if (Match(tok, "memset ( %var% , %num% , sizeof ( struct %type% ) )"))
|
2008-11-09 08:19:53 +01:00
|
|
|
type = Tokenizer::getstr(tok, 9);
|
2008-03-28 08:18:03 +01:00
|
|
|
else if (Match(tok, "memset ( & %var% , %num% , sizeof ( struct %type% ) )"))
|
2008-11-09 08:19:53 +01:00
|
|
|
type = Tokenizer::getstr(tok, 10);
|
2008-04-18 19:18:22 +02:00
|
|
|
else if (Match(tok, "%type% ( %var% , %var% , sizeof ( %type% ) )"))
|
2008-11-09 08:19:53 +01:00
|
|
|
type = Tokenizer::getstr(tok, 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-09 08:19:53 +01:00
|
|
|
if (Tokenizer::findtoken(tokens,pattern1))
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2008-11-13 23:39:47 +01:00
|
|
|
ostr << FileLine(tok, _tokenizer) << ": Using '" << tok->str << "' on class.";
|
2007-05-24 15:07:30 +02:00
|
|
|
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-09 08:19:53 +01:00
|
|
|
for (const TOKEN *tstruct = Tokenizer::findtoken(tokens, pattern2); tstruct; tstruct = tstruct->next)
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
|
|
|
if (tstruct->str[0] == '}')
|
|
|
|
break;
|
|
|
|
|
2008-03-28 08:18:03 +01:00
|
|
|
if (Match(tstruct, "std :: %type% %var% ;"))
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2008-11-13 23:39:47 +01:00
|
|
|
ostr << FileLine(tok, _tokenizer) << ": Using '" << tok->str << "' on struct that contains a 'std::" << Tokenizer::getstr(tstruct,2) << "'";
|
2007-05-24 15:07:30 +02:00
|
|
|
ReportErr(ostr.str());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// 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-09 08:19:53 +01:00
|
|
|
if (const TOKEN *tok = Tokenizer::findtoken(tokens,pattern))
|
2007-05-24 15:07:30 +02:00
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
2008-11-13 23:39:47 +01:00
|
|
|
ostr << FileLine(tok, _tokenizer) << ": 'operator=' should return something";
|
2007-05-24 15:07:30 +02:00
|
|
|
ReportErr(ostr.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|