cppcheck/CommonCheck.cpp

101 lines
2.8 KiB
C++
Raw Normal View History

//---------------------------------------------------------------------------
#include "CommonCheck.h"
#include "tokenize.h"
#include <iostream>
#include <sstream>
#include <list>
#include <algorithm>
//---------------------------------------------------------------------------
bool HasErrors;
bool OnlyReportUniqueErrors;
2008-02-16 16:46:32 +01:00
std::ostringstream errout;
//---------------------------------------------------------------------------
std::string FileLine(const TOKEN *tok)
{
std::ostringstream ostr;
ostr << "[" << Files[tok->FileIndex] << ":" << tok->linenr << "]";
return ostr.str();
}
//---------------------------------------------------------------------------
std::list<std::string> ErrorList;
2008-03-22 18:09:08 +01:00
void ReportErr(const std::string &errmsg)
{
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;
HasErrors = true;
}
//---------------------------------------------------------------------------
bool IsName(const char str[])
{
return (str[0]=='_' || std::isalpha(str[0]));
}
//---------------------------------------------------------------------------
bool IsNumber(const char str[])
{
return std::isdigit(str[0]);
}
//---------------------------------------------------------------------------
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;
}
//---------------------------------------------------------------------------
2008-03-23 08:57:27 +01:00
bool setindentlevel( const TOKEN *tok, int &indentlevel, int endlevel )
{
if ( tok->str[0] == '{' )
indentlevel++;
else if ( tok->str[0] == '}' )
indentlevel--;
2008-03-23 08:57:27 +01:00
return bool(tok->str[0]=='}' && indentlevel<=endlevel);
}
//---------------------------------------------------------------------------
2008-03-23 14:27:34 +01:00
void GotoNextStatement( const TOKEN **tok )
{
// Goto end of statement..
while ( *tok && ! strchr("{};", (*tok)->str[0]) )
*tok = (*tok)->next;
// Goto next statement
if ( *tok )
*tok = (*tok)->next;
}
//---------------------------------------------------------------------------
void FindMatchingTokenInScope( const TOKEN **tok, const char pattern[], int &indentlevel )
{
while ( *tok )
{
if ( setindentlevel( *tok, indentlevel, -1 ) )
*tok = NULL;
else if ( match( *tok, pattern ) )
return;
else
*tok = (*tok)->next;
}
}
//---------------------------------------------------------------------------