2007-05-24 15:09:23 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#include "CommonCheck.h"
|
|
|
|
#include "tokenize.h"
|
2008-08-20 09:32:07 +02:00
|
|
|
#include <stdlib.h> // free
|
2007-05-24 15:09:23 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
2008-02-22 15:30:43 +01:00
|
|
|
#include <list>
|
2008-09-10 20:15:33 +02:00
|
|
|
#include <algorithm>
|
2008-08-30 20:29:37 +02:00
|
|
|
#include <cstring>
|
2008-09-11 19:03:58 +02:00
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#include <ctype.h>
|
|
|
|
#endif
|
2007-05-24 15:09:23 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2008-03-29 10:44:23 +01:00
|
|
|
extern bool CheckCodingStyle;
|
2008-02-22 15:30:43 +01:00
|
|
|
bool OnlyReportUniqueErrors;
|
2008-02-16 16:46:32 +01:00
|
|
|
std::ostringstream errout;
|
2008-03-28 18:40:24 +01:00
|
|
|
static std::list<const TOKEN *> FunctionList;
|
2008-03-29 10:44:23 +01:00
|
|
|
|
2008-08-07 20:44:05 +02:00
|
|
|
class GlobalFunction
|
2008-03-29 10:44:23 +01:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
unsigned int _FileId;
|
|
|
|
std::string _FuncName;
|
|
|
|
|
|
|
|
public:
|
2008-08-07 20:44:05 +02:00
|
|
|
GlobalFunction( const unsigned int FileId, const char FuncName[] )
|
2008-03-29 10:44:23 +01:00
|
|
|
{
|
|
|
|
_FileId = FileId;
|
|
|
|
_FuncName = FuncName;
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned int file_id() const { return _FileId; }
|
|
|
|
const std::string &name() const { return _FuncName; }
|
|
|
|
};
|
|
|
|
|
2008-08-07 20:44:05 +02:00
|
|
|
static std::list< GlobalFunction > GlobalFunctions;
|
|
|
|
static std::list< GlobalFunction > UsedGlobalFunctions;
|
2008-03-29 10:44:23 +01:00
|
|
|
|
2007-07-18 08:12:16 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2007-05-24 15:09:23 +02:00
|
|
|
|
2008-03-16 14:17:43 +01:00
|
|
|
std::string FileLine(const TOKEN *tok)
|
2007-05-24 15:09:23 +02:00
|
|
|
{
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << "[" << Files[tok->FileIndex] << ":" << tok->linenr << "]";
|
|
|
|
return ostr.str();
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-04-06 08:26:11 +02:00
|
|
|
bool SameFileName( const char fname1[], const char fname2[] )
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
return bool( strcmp(fname1, fname2) == 0 );
|
|
|
|
#endif
|
2008-09-10 20:15:33 +02:00
|
|
|
#ifdef __GNUC__
|
2008-04-06 08:26:11 +02:00
|
|
|
return bool( strcasecmp(fname1, fname2) == 0 );
|
2008-09-11 19:03:58 +02:00
|
|
|
#endif
|
|
|
|
#ifdef __BORLANDC__
|
2008-09-10 20:15:33 +02:00
|
|
|
return bool( stricmp(fname1, fname2) == 0 );
|
2008-04-06 08:26:11 +02:00
|
|
|
#endif
|
2008-09-11 19:03:58 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
return bool( _stricmp(fname1, fname2) == 0 );
|
|
|
|
#endif
|
2008-04-06 08:26:11 +02:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-02-22 15:30:43 +01:00
|
|
|
std::list<std::string> ErrorList;
|
|
|
|
|
2008-03-22 18:09:08 +01:00
|
|
|
void ReportErr(const std::string &errmsg)
|
2007-05-24 15:09:23 +02:00
|
|
|
{
|
2008-02-22 15:30:43 +01:00
|
|
|
if ( OnlyReportUniqueErrors )
|
|
|
|
{
|
|
|
|
if ( std::find( ErrorList.begin(), ErrorList.end(), errmsg ) != ErrorList.end() )
|
|
|
|
return;
|
|
|
|
ErrorList.push_back( errmsg );
|
|
|
|
}
|
2008-02-16 16:46:32 +01:00
|
|
|
errout << errmsg << std::endl;
|
2007-05-24 15:09:23 +02:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2007-05-25 08:45:47 +02:00
|
|
|
bool IsName(const char str[])
|
|
|
|
{
|
2008-09-11 19:03:58 +02:00
|
|
|
return bool(str[0]=='_' || isalpha(str[0]));
|
2007-05-25 08:45:47 +02:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool IsNumber(const char str[])
|
|
|
|
{
|
2008-09-11 19:03:58 +02:00
|
|
|
return bool(isdigit(str[0]) != 0);
|
2007-05-25 08:45:47 +02:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2007-07-20 08:20:31 +02:00
|
|
|
bool IsStandardType(const char str[])
|
|
|
|
{
|
|
|
|
if (!str)
|
|
|
|
return false;
|
|
|
|
bool Ret = false;
|
|
|
|
const char *type[] = {"bool","char","short","int","long","float","double",0};
|
|
|
|
for (int i = 0; type[i]; i++)
|
|
|
|
Ret |= (strcmp(str,type[i])==0);
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
2007-05-25 08:45:47 +02:00
|
|
|
|
2008-03-29 10:44:23 +01:00
|
|
|
void FillFunctionList(const unsigned int file_id)
|
2008-03-23 17:18:31 +01:00
|
|
|
{
|
2008-03-28 18:40:24 +01:00
|
|
|
FunctionList.clear();
|
|
|
|
|
2008-03-29 10:44:23 +01:00
|
|
|
std::list<const char *> _usedfunc;
|
|
|
|
if ( file_id == 0 )
|
|
|
|
{
|
|
|
|
GlobalFunctions.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool staticfunc = false;
|
|
|
|
bool classfunc = false;
|
|
|
|
|
2008-03-23 17:18:31 +01:00
|
|
|
int indentlevel = 0;
|
2008-03-28 08:18:03 +01:00
|
|
|
for ( const TOKEN *tok = tokens; tok; tok = tok->next )
|
2008-03-23 17:18:31 +01:00
|
|
|
{
|
|
|
|
if ( tok->str[0] == '{' )
|
|
|
|
indentlevel++;
|
|
|
|
|
|
|
|
else if ( tok->str[0] == '}' )
|
|
|
|
indentlevel--;
|
|
|
|
|
2008-03-29 10:44:23 +01:00
|
|
|
|
|
|
|
if (indentlevel > 0)
|
|
|
|
{
|
|
|
|
if ( CheckCodingStyle )
|
|
|
|
{
|
|
|
|
const char *funcname = 0;
|
|
|
|
|
|
|
|
if ( Match(tok,"%var% (") )
|
|
|
|
funcname = tok->str;
|
2008-03-29 18:27:02 +01:00
|
|
|
else if ( Match(tok, "= %var% ;") ||
|
|
|
|
Match(tok, "= %var% ,") )
|
2008-03-29 10:44:23 +01:00
|
|
|
funcname = tok->next->str;
|
|
|
|
|
|
|
|
if ( std::find(_usedfunc.begin(), _usedfunc.end(), funcname) == _usedfunc.end() )
|
|
|
|
_usedfunc.push_back( funcname );
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strchr("};", tok->str[0]))
|
|
|
|
staticfunc = classfunc = false;
|
|
|
|
|
|
|
|
else if ( strcmp( tok->str, "static" ) == 0 )
|
|
|
|
staticfunc = true;
|
|
|
|
|
|
|
|
else if ( strcmp( tok->str, "::" ) == 0 )
|
|
|
|
classfunc = true;
|
|
|
|
|
|
|
|
else if (Match(tok, "%var% ("))
|
2008-03-23 17:18:31 +01:00
|
|
|
{
|
|
|
|
// Check if this is the first token of a function implementation..
|
|
|
|
for ( const TOKEN *tok2 = tok; tok2; tok2 = tok2->next )
|
|
|
|
{
|
|
|
|
if ( tok2->str[0] == ';' )
|
2008-03-24 08:24:49 +01:00
|
|
|
{
|
|
|
|
tok = tok2;
|
2008-03-23 17:18:31 +01:00
|
|
|
break;
|
2008-03-24 08:24:49 +01:00
|
|
|
}
|
2008-03-28 08:18:03 +01:00
|
|
|
|
|
|
|
else if ( tok2->str[0] == '{' )
|
|
|
|
{
|
2008-03-23 17:18:31 +01:00
|
|
|
break;
|
2008-03-28 08:18:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else if ( tok2->str[0] == ')' )
|
|
|
|
{
|
|
|
|
if ( Match(tok2, ") {") )
|
|
|
|
{
|
2008-03-29 10:44:23 +01:00
|
|
|
if (CheckCodingStyle && !staticfunc && !classfunc && tok->FileIndex==0)
|
2008-08-07 20:44:05 +02:00
|
|
|
GlobalFunctions.push_back( GlobalFunction(file_id, tok->str) );
|
2008-03-28 08:18:03 +01:00
|
|
|
FunctionList.push_back( tok );
|
|
|
|
tok = tok2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tok = tok2;
|
|
|
|
while (tok->next && !strchr(";{", tok->next->str[0]))
|
|
|
|
tok = tok->next;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-03-23 17:18:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-03-29 10:44:23 +01:00
|
|
|
|
|
|
|
for (std::list<const char *>::const_iterator it = _usedfunc.begin(); it != _usedfunc.end(); ++it)
|
|
|
|
{
|
|
|
|
if ( *it != 0 )
|
|
|
|
{
|
2008-08-07 20:44:05 +02:00
|
|
|
UsedGlobalFunctions.push_back( GlobalFunction(file_id, *it) );
|
2008-03-29 10:44:23 +01:00
|
|
|
}
|
|
|
|
}
|
2008-03-28 08:18:03 +01:00
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const TOKEN *GetFunctionTokenByName( const char funcname[] )
|
|
|
|
{
|
|
|
|
std::list<const TOKEN *>::const_iterator it;
|
|
|
|
for ( it = FunctionList.begin(); it != FunctionList.end(); it++ )
|
|
|
|
{
|
|
|
|
if ( strcmp( (*it)->str, funcname ) == 0 )
|
|
|
|
{
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
}
|
2008-03-23 17:18:31 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-03-28 08:18:03 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-03-29 10:44:23 +01:00
|
|
|
void CheckGlobalFunctionUsage(const std::vector<std::string> &filenames)
|
|
|
|
{
|
|
|
|
// Iterator for GlobalFunctions
|
2008-08-07 20:44:05 +02:00
|
|
|
std::list<GlobalFunction>::const_iterator func;
|
2008-03-29 10:44:23 +01:00
|
|
|
|
|
|
|
// Iterator for UsedGlobalFunctions
|
2008-08-07 20:44:05 +02:00
|
|
|
std::list<GlobalFunction>::const_iterator usedfunc;
|
2008-03-29 10:44:23 +01:00
|
|
|
|
|
|
|
// Check that every function in GlobalFunctions are used
|
|
|
|
for ( func = GlobalFunctions.begin(); func != GlobalFunctions.end(); func++ )
|
|
|
|
{
|
2008-05-05 18:58:09 +02:00
|
|
|
const std::string &funcname = func->name();
|
|
|
|
|
|
|
|
if ( funcname == "main" || funcname == "WinMain" )
|
2008-03-29 10:44:23 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check if this global function is used in any of the other files..
|
|
|
|
bool UsedOtherFile = false;
|
|
|
|
bool UsedAnyFile = false;
|
|
|
|
for ( usedfunc = UsedGlobalFunctions.begin(); usedfunc != UsedGlobalFunctions.end(); usedfunc++ )
|
|
|
|
{
|
2008-05-05 18:58:09 +02:00
|
|
|
if ( funcname == usedfunc->name() )
|
2008-03-29 10:44:23 +01:00
|
|
|
{
|
|
|
|
UsedAnyFile = true;
|
2008-05-05 18:58:09 +02:00
|
|
|
if (func->file_id() != usedfunc->file_id())
|
|
|
|
{
|
|
|
|
UsedOtherFile = true;
|
|
|
|
break;
|
|
|
|
}
|
2008-03-29 10:44:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! UsedAnyFile )
|
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
2008-05-05 18:58:09 +02:00
|
|
|
errmsg << "[" << filenames[func->file_id()] << "]: "
|
|
|
|
<< "The function '" << func->name() << "' is never used.";
|
2008-03-29 10:44:23 +01:00
|
|
|
ReportErr( errmsg.str() );
|
|
|
|
}
|
|
|
|
else if ( ! UsedOtherFile )
|
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
2008-05-05 18:58:09 +02:00
|
|
|
errmsg << "[" << filenames[func->file_id()] << "]: "
|
|
|
|
<< "The linkage of the function '" << func->name() << "' can be local (static) instead of global";
|
2008-03-29 10:44:23 +01:00
|
|
|
ReportErr( errmsg.str() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2008-03-28 18:40:24 +01:00
|
|
|
bool Match(const TOKEN *tok, const char pattern[], const char *varname1[], const char *varname2[])
|
2008-03-28 08:18:03 +01:00
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const char *p = pattern;
|
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
// Skip spaces in pattern..
|
|
|
|
while ( *p == ' ' )
|
|
|
|
p++;
|
|
|
|
|
|
|
|
// Extract token from pattern..
|
|
|
|
char str[50];
|
|
|
|
char *s = str;
|
|
|
|
while (*p && *p!=' ')
|
|
|
|
{
|
|
|
|
*s = *p;
|
|
|
|
s++;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
*s = 0;
|
|
|
|
|
|
|
|
// No token => Success!
|
|
|
|
if (str[0] == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Any symbolname..
|
|
|
|
if (strcmp(str,"%var%")==0 || strcmp(str,"%type%")==0)
|
|
|
|
{
|
|
|
|
if (!IsName(tok->str))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Variable name..
|
2008-03-28 18:40:24 +01:00
|
|
|
else if (strcmp(str,"%var1%")==0 || strcmp(str,"%var2%")==0)
|
2008-03-28 08:18:03 +01:00
|
|
|
{
|
2008-03-28 18:40:24 +01:00
|
|
|
const char **varname = (strcmp(str,"%var1%")==0) ? varname1 : varname2;
|
|
|
|
|
|
|
|
if ( ! varname )
|
|
|
|
return false;
|
|
|
|
|
2008-03-28 08:18:03 +01:00
|
|
|
if (strcmp(tok->str, varname[0]) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for ( int i = 1; varname[i]; i++ )
|
|
|
|
{
|
|
|
|
if ( ! gettok(tok, 2) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ( strcmp(getstr(tok, 1), ".") )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ( strcmp(getstr(tok, 2), varname[i]) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
tok = gettok(tok, 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (strcmp(str,"%num%")==0)
|
|
|
|
{
|
|
|
|
if ( ! IsNumber(tok->str) )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
else if (strcmp(str,"%str%")==0)
|
|
|
|
{
|
|
|
|
if ( tok->str[0] != '\"' )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-04-02 11:37:05 +02:00
|
|
|
// [.. => search for a one-character token..
|
|
|
|
else if (str[0]=='[' && strchr(str, ']') && tok->str[1] == 0)
|
|
|
|
{
|
|
|
|
*strrchr(str, ']') = 0;
|
|
|
|
if ( strchr( str + 1, tok->str[0] ) == 0 )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-03-28 08:18:03 +01:00
|
|
|
else if (strcmp(str, tok->str) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
tok = tok->next;
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The end of the pattern has been reached and nothing wrong has been found
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
2008-03-23 17:18:31 +01:00
|
|
|
|
2008-08-16 14:44:46 +02:00
|
|
|
const TOKEN *findmatch(const TOKEN *tok, const char pattern[], const char *varname1[], const char *varname2[])
|
|
|
|
{
|
|
|
|
for ( ; tok; tok = tok->next)
|
|
|
|
{
|
|
|
|
if ( Match(tok, pattern, varname1, varname2) )
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void deleteTokens(TOKEN *tok)
|
|
|
|
{
|
|
|
|
while (tok)
|
|
|
|
{
|
2008-08-20 09:32:07 +02:00
|
|
|
TOKEN *next = tok->next;
|
2008-08-19 06:53:41 +02:00
|
|
|
free(tok->str);
|
2008-08-16 14:44:46 +02:00
|
|
|
delete tok;
|
|
|
|
tok = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|