Removed false positives and also duplicate error messages.

This commit is contained in:
Daniel Marjamäki 2008-02-22 14:30:43 +00:00
parent 942c48474c
commit a6696d6f7d
4 changed files with 17 additions and 56 deletions

View File

@ -242,59 +242,6 @@ void WarningIf()
}
ReportErr(ostr.str());
}
// Search for 'if (condition) flag = true;'
bool newstatement = false;
for (TOKEN *tok = tokens; tok; tok = tok->next)
{
if (!newstatement || strcmp(tok->str,"if"))
{
newstatement = (strchr("{};",tok->str[0]));
continue;
}
int parlevel = 0;
for (TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next)
{
if (tok2->str[0]=='(')
parlevel++;
else if (tok2->str[0]==')')
{
parlevel--;
if (parlevel<=0)
{
if ( strcmp(getstr(tok2,5), "else") == 0 )
{
if ( match(tok2->next, "var = true ; else var = false ;") )
{
std::ostringstream ostr;
ostr << FileLine(tok) << ": Found \"if (condition) var=true; else var=false;\", it can be rewritten as \"var = (condition);\"";
ReportErr(ostr.str());
}
}
else if ( match(tok2->next, "var = true ;") )
{
std::ostringstream ostr;
ostr << FileLine(tok) << ": Found \"if (condition) var = true;\", it can be rewritten as \"var |= (condition);\"";
ReportErr(ostr.str());
}
else if ( match(tok2->next, "var = false ;") )
{
std::ostringstream ostr;
ostr << FileLine(tok) << ": Found \"if (condition) var = false;\", it can be rewritten as \"var &= (!condition);\"";
ReportErr(ostr.str());
}
break;
}
}
}
newstatement = (strchr("{};",tok->str[0]));
}
}
//---------------------------------------------------------------------------

View File

@ -3,8 +3,11 @@
#include "tokenize.h"
#include <iostream>
#include <sstream>
#include <list>
#include <algorithm>
//---------------------------------------------------------------------------
bool HasErrors;
bool OnlyReportUniqueErrors;
std::ostringstream errout;
//---------------------------------------------------------------------------
@ -16,8 +19,16 @@ std::string FileLine(TOKEN *tok)
}
//---------------------------------------------------------------------------
std::list<std::string> ErrorList;
void ReportErr(const std::string errmsg)
{
if ( OnlyReportUniqueErrors )
{
if ( std::find( ErrorList.begin(), ErrorList.end(), errmsg ) != ErrorList.end() )
return;
ErrorList.push_back( errmsg );
}
errout << errmsg << std::endl;
HasErrors = true;
}

View File

@ -9,6 +9,8 @@
struct TOKEN;
std::string FileLine(TOKEN *tok);
extern bool OnlyReportUniqueErrors;
void ReportErr(const std::string errmsg);
extern std::ostringstream errout;

View File

@ -169,6 +169,7 @@ extern bool HasErrors;
static void CppCheck(const char FileName[])
{
HasErrors = false;
OnlyReportUniqueErrors = true;
std::cout << "Checking " << FileName << "...\n";
@ -190,9 +191,9 @@ static void CppCheck(const char FileName[])
CheckUnsignedDivision();
// Including header which is not needed
if ( CheckCodingStyle )
WarningIncludeHeader();
// Including header which is not needed (too many false positives)
//if ( CheckCodingStyle )
// WarningIncludeHeader();
SimplifyTokenList();