Refactoring of obsolete functions checks. Ticket: #1940

This commit is contained in:
Sbastien Debrard 2010-08-15 21:22:33 +02:00 committed by Daniel Marjamäki
parent 8d52c33790
commit 7398453cb0
5 changed files with 194 additions and 312 deletions

View File

@ -38,216 +38,15 @@ void CheckObsoleteFunctions::obsoleteFunctions()
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{ {
if (tok->strAt(1) == "bsd_signal" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::") std::list< std::pair<const std::string, const std::string> >::const_iterator it (_obsoleteFunctions.begin()), itend(_obsoleteFunctions.end());
for(;it!=itend;++it) {
if (tok->strAt(1) == it->first && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{ {
obsoleteFunctionbsd_signal(tok->tokAt(1)); reportError(tok->tokAt(1), Severity::style, "obsoleteFunctions"+it->first, it->second);
break;
} }
else if (tok->strAt(1) == "gethostbyaddr" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{
obsoleteFunctiongethostbyaddr(tok->tokAt(1));
}
else if (tok->strAt(1) == "gethostbyname" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{
obsoleteFunctiongethostbyname(tok->tokAt(1));
}
else if (tok->strAt(1) == "usleep" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{
obsoleteFunctionusleep(tok->tokAt(1));
}
else if (tok->strAt(1) == "bcmp" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{
obsoleteFunctionbcmp(tok->tokAt(1));
}
else if (tok->strAt(1) == "bcopy" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{
obsoleteFunctionbcopy(tok->tokAt(1));
}
else if (tok->strAt(1) == "bzero" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{
obsoleteFunctionbzero(tok->tokAt(1));
}
else if (tok->strAt(1) == "ecvt" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{
obsoleteFunctionecvt(tok->tokAt(1));
}
else if (tok->strAt(1) == "fcvt" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{
obsoleteFunctionfcvt(tok->tokAt(1));
}
else if (tok->strAt(1) == "gcvt" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{
obsoleteFunctiongcvt(tok->tokAt(1));
}
else if (tok->strAt(1) == "ftime" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{
obsoleteFunctionftime(tok->tokAt(1));
}
else if (tok->strAt(1) == "getcontext" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{
obsoleteFunctiongetcontext(tok->tokAt(1));
}
else if (tok->strAt(1) == "makecontext" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{
obsoleteFunctionmakecontext(tok->tokAt(1));
}
else if (tok->strAt(1) == "swapcontext" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{
obsoleteFunctionswapcontext(tok->tokAt(1));
}
else if (tok->strAt(1) == "getwd" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{
obsoleteFunctiongetwd(tok->tokAt(1));
}
else if (tok->strAt(1) == "index" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{
obsoleteFunctionindex(tok->tokAt(1));
}
else if (tok->strAt(1) == "pthread_attr_getstackaddr" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{
obsoleteFunctionpthread_attr_getstackaddr(tok->tokAt(1));
}
else if (tok->strAt(1) == "pthread_attr_setstackaddr" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{
obsoleteFunctionpthread_attr_setstackaddr(tok->tokAt(1));
}
else if (tok->strAt(1) == "rindex" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{
obsoleteFunctionrindex(tok->tokAt(1));
}
else if (tok->strAt(1) == "scalb" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{
obsoleteFunctionscalb(tok->tokAt(1));
}
else if (tok->strAt(1) == "ualarm" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{
obsoleteFunctionualarm(tok->tokAt(1));
}
else if (tok->strAt(1) == "vfork" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{
obsoleteFunctionvfork(tok->tokAt(1));
}
else if (tok->strAt(1) == "wcswcs" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
{
obsoleteFunctionwcswcs(tok->tokAt(1));
} }
} }
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void CheckObsoleteFunctions::obsoleteFunctionbsd_signal(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctionbsd_signal", "Found obsolete function 'bsd_signal'. It is recommended that new applications use the 'sigaction' function");
}
void CheckObsoleteFunctions::obsoleteFunctiongethostbyaddr(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctiongethostbyaddr", "Found obsolete function 'gethostbyaddr'. It is recommended that new applications use the 'getaddrinfo' function");
}
void CheckObsoleteFunctions::obsoleteFunctiongethostbyname(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctiongethostbyname", "Found obsolete function 'gethostbyname'. It is recommended that new applications use the 'getnameinfo' function");
}
void CheckObsoleteFunctions::obsoleteFunctionusleep(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctionusleep", "Found obsolete function 'usleep'. It is recommended that new applications use the 'nanosleep' or 'setitimer' function");
}
void CheckObsoleteFunctions::obsoleteFunctionbcmp(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctionbcmp", "Found obsolete function 'bcmp'. It is recommended that new applications use the 'memcmp' function");
}
void CheckObsoleteFunctions::obsoleteFunctionbcopy(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctionbcopy", "Found obsolete function 'bcopy'. It is recommended that new applications use the 'memmove' function");
}
void CheckObsoleteFunctions::obsoleteFunctionbzero(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctionbzero", "Found obsolete function 'bzero'. It is recommended that new applications use the 'memset' function");
}
void CheckObsoleteFunctions::obsoleteFunctionecvt(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctionecvt", "Found obsolete function 'ecvt'. It is recommended that new applications use the 'sprintf' function");
}
void CheckObsoleteFunctions::obsoleteFunctionfcvt(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctionfcvt", "Found obsolete function 'fcvt'. It is recommended that new applications use the 'sprintf' function");
}
void CheckObsoleteFunctions::obsoleteFunctiongcvt(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctiongcvt", "Found obsolete function 'gcvt'. It is recommended that new applications use the 'sprintf' function");
}
void CheckObsoleteFunctions::obsoleteFunctionftime(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctionftime", "Found obsolete function 'ftime'. It is recommended that new applications use the 'ftime' function. Realtime applications should use ''clock_gettime'' to determine the current time");
}
void CheckObsoleteFunctions::obsoleteFunctiongetcontext(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctiongetcontext", "Found obsolete function 'getcontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads");
}
void CheckObsoleteFunctions::obsoleteFunctionmakecontext(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctionmakecontext", "Found obsolete function 'makecontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads");
}
void CheckObsoleteFunctions::obsoleteFunctionswapcontext(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctionswapcontext", "Found obsolete function 'swapcontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads");
}
void CheckObsoleteFunctions::obsoleteFunctiongetwd(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctiongetwd", "Found obsolete function 'getwd'. It is recommended that new applications use the 'getcwd' function");
}
void CheckObsoleteFunctions::obsoleteFunctionindex(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctionindex", "Found obsolete function 'index'. It is recommended to use the function 'strchr' instead");
}
void CheckObsoleteFunctions::obsoleteFunctionpthread_attr_getstackaddr(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctionpthread_attr_getstackaddr", "Found obsolete function 'pthread_attr_getstackaddr'.It is recommended that new applications use the 'pthread_attr_getstack' function");
}
void CheckObsoleteFunctions::obsoleteFunctionpthread_attr_setstackaddr(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctionpthread_attr_setstackaddr", "Found obsolete function 'pthread_attr_setstackaddr'.It is recommended that new applications use the 'pthread_attr_setstack' function");
}
void CheckObsoleteFunctions::obsoleteFunctionrindex(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctionrindex", "Found obsolete function 'rindex'. It is recommended to use the function 'strrchr' instead");
}
void CheckObsoleteFunctions::obsoleteFunctionscalb(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctionscalb", "Found obsolete function 'scalb'.It is recommended to use either 'scalbln', 'scalblnf()' or 'scalblnl' instead of this function");
}
void CheckObsoleteFunctions::obsoleteFunctionualarm(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctionualarm", "Found obsolete function 'ualarm'.It is recommended to use either 'timer_create', 'timer_delete', 'timer_getoverrun', 'timer_gettime', or 'timer_settime' instead of this function");
}
void CheckObsoleteFunctions::obsoleteFunctionvfork(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctionvfork", "Found obsolete function 'vfork'. It is recommended to use the function 'fork' instead");
}
void CheckObsoleteFunctions::obsoleteFunctionwcswcs(const Token *tok)
{
reportError(tok, Severity::style, "obsoleteFunctionwcswcs", "Found obsolete function 'wcswcs'. It is recommended to use the function 'wcsstr' instead");
}

View File

@ -23,6 +23,9 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include "check.h" #include "check.h"
#include <string>
#include <list>
/// @addtogroup Checks /// @addtogroup Checks
/// @{ /// @{
@ -36,12 +39,12 @@ class CheckObsoleteFunctions : public Check
public: public:
/** This constructor is used when registering the CheckObsoleteFunctions */ /** This constructor is used when registering the CheckObsoleteFunctions */
CheckObsoleteFunctions() : Check() CheckObsoleteFunctions() : Check()
{ } { initObsoleteFunctions(); }
/** This constructor is used when running checks. */ /** This constructor is used when running checks. */
CheckObsoleteFunctions(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) CheckObsoleteFunctions(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(tokenizer, settings, errorLogger) : Check(tokenizer, settings, errorLogger)
{ } { initObsoleteFunctions(); }
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{ {
@ -53,100 +56,56 @@ public:
void obsoleteFunctions(); void obsoleteFunctions();
private: private:
/** Report Error : Using obsolete function 'bsd_signal' */ /* function name / error message */
void obsoleteFunctionbsd_signal(const Token *tok); std::list< std::pair< const std::string, const std::string> > _obsoleteFunctions;
/** Report Error : Using obsolete function 'gethostbyaddr' */ /** init obsolete functions list ' */
void obsoleteFunctiongethostbyaddr(const Token*); void initObsoleteFunctions() {
_obsoleteFunctions.push_back(std::make_pair("bsd_signal","Found obsolete function 'bsd_signal'. It is recommended that new applications use the 'sigaction' function"));
/** Report Error : Using obsolete function 'gethostbyname' */ _obsoleteFunctions.push_back(std::make_pair("gethostbyaddr","Found obsolete function 'gethostbyaddr'. It is recommended that new applications use the 'getaddrinfo' function"));
void obsoleteFunctiongethostbyname(const Token*); _obsoleteFunctions.push_back(std::make_pair("gethostbyname","Found obsolete function 'gethostbyname'. It is recommended that new applications use the 'getnameinfo' function"));
/** Report Error : Using obsolete function 'usleep' */ _obsoleteFunctions.push_back(std::make_pair("usleep","Found obsolete function 'usleep'. It is recommended that new applications use the 'nanosleep' or 'setitimer' function"));
void obsoleteFunctionusleep(const Token*);
/** Report Error : Using obsolete function 'bcmp' */ _obsoleteFunctions.push_back(std::make_pair("bcmp","Found obsolete function 'bcmp'. It is recommended that new applications use the 'memcmp' function"));
void obsoleteFunctionbcmp(const Token *tok); _obsoleteFunctions.push_back(std::make_pair("bcopy","Found obsolete function 'bcopy'. It is recommended that new applications use the 'memmove' function"));
_obsoleteFunctions.push_back(std::make_pair("bzero","Found obsolete function 'bzero'. It is recommended that new applications use the 'memset' function"));
/** Report Error : Using obsolete function 'bcopy' */ _obsoleteFunctions.push_back(std::make_pair("ecvt","Found obsolete function 'ecvt'. It is recommended that new applications use the 'sprintf' function"));
void obsoleteFunctionbcopy(const Token *tok); _obsoleteFunctions.push_back(std::make_pair("fcvt","Found obsolete function 'fcvt'. It is recommended that new applications use the 'sprintf' function"));
_obsoleteFunctions.push_back(std::make_pair("gcvt","Found obsolete function 'gcvt'. It is recommended that new applications use the 'sprintf' function"));
/** Report Error : Using obsolete function 'bzero' */ _obsoleteFunctions.push_back(std::make_pair("ftime","Found obsolete function 'ftime'. It is recommended that new applications use the 'ftime' function. Realtime applications should use ''clock_gettime'' to determine the current time"));
void obsoleteFunctionbzero(const Token *tok);
/** Report Error : Using obsolete function 'ecvt' */ _obsoleteFunctions.push_back(std::make_pair("getcontext","Found obsolete function 'getcontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads"));
void obsoleteFunctionecvt(const Token *tok); _obsoleteFunctions.push_back(std::make_pair("makecontext","Found obsolete function 'makecontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads"));
_obsoleteFunctions.push_back(std::make_pair("swapcontext","Found obsolete function 'swapcontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads"));
/** Report Error : Using obsolete function 'fcvt' */ _obsoleteFunctions.push_back(std::make_pair("getwd","Found obsolete function 'getwd'. It is recommended that new applications use the 'getcwd' function"));
void obsoleteFunctionfcvt(const Token *tok);
/** Report Error : Using obsolete function 'gcvt' */ _obsoleteFunctions.push_back(std::make_pair("index","Found obsolete function 'index'. It is recommended to use the function 'strchr' instead"));
void obsoleteFunctiongcvt(const Token *tok); _obsoleteFunctions.push_back(std::make_pair("rindex","Found obsolete function 'rindex'. It is recommended to use the function 'strrchr' instead"));
/** Report Error : Using obsolete function 'ftime' */ _obsoleteFunctions.push_back(std::make_pair("pthread_attr_getstackaddr","Found obsolete function 'pthread_attr_getstackaddr'.It is recommended that new applications use the 'pthread_attr_getstack' function"));
void obsoleteFunctionftime(const Token *tok); _obsoleteFunctions.push_back(std::make_pair("pthread_attr_setstackaddr","Found obsolete function 'pthread_attr_setstackaddr'.It is recommended that new applications use the 'pthread_attr_setstack' function"));
/** Report Error : Using obsolete function 'getcontext' */ _obsoleteFunctions.push_back(std::make_pair("scalbln","Found obsolete function 'scalb'.It is recommended to use either 'scalbln', 'scalblnf' or 'scalblnl' instead of this function"));
void obsoleteFunctiongetcontext(const Token *tok);
/** Report Error : Using obsolete function 'makecontext' */ _obsoleteFunctions.push_back(std::make_pair("ualarm","Found obsolete function 'ualarm'.It is recommended to use either 'timer_create', 'timer_delete', 'timer_getoverrun', 'timer_gettime', or 'timer_settime' instead of this function"));
void obsoleteFunctionmakecontext(const Token *tok);
/** Report Error : Using obsolete function 'swapcontext' */ _obsoleteFunctions.push_back(std::make_pair("vfork","Found obsolete function 'vfork'. It is recommended to use the function 'fork' instead"));
void obsoleteFunctionswapcontext(const Token *tok);
/** Report Error : Using obsolete function 'getwd' */ _obsoleteFunctions.push_back(std::make_pair("wcswcs","Found obsolete function 'wcswcs'. It is recommended to use the function 'wcsstr' instead"));
void obsoleteFunctiongetwd(const Token *tok);
/** Report Error : Using obsolete function 'index' */ }
void obsoleteFunctionindex(const Token *tok);
/** Report Error : Using obsolete function 'pthread_attr_getstackaddr' */
void obsoleteFunctionpthread_attr_getstackaddr(const Token *tok);
/** Report Error : Using obsolete function 'pthread_attr_setstackaddr' */
void obsoleteFunctionpthread_attr_setstackaddr(const Token *tok);
/** Report Error : Using obsolete function 'rindex' */
void obsoleteFunctionrindex(const Token *tok);
/** Report Error : Using obsolete function 'scalb' */
void obsoleteFunctionscalb(const Token *tok);
/** Report Error : Using obsolete function 'ualarm' */
void obsoleteFunctionualarm(const Token *tok);
/** Report Error : Using obsolete function 'vfork' */
void obsoleteFunctionvfork(const Token *tok);
/** Report Error : Using obsolete function 'wcswcs' */
void obsoleteFunctionwcswcs(const Token *tok);
void getErrorMessages() void getErrorMessages()
{ {
obsoleteFunctionbsd_signal(0); std::list< std::pair<const std::string, const std::string> >::const_iterator it (_obsoleteFunctions.begin()), itend(_obsoleteFunctions.end());
obsoleteFunctiongethostbyaddr(0); for(;it!=itend;++it) {
obsoleteFunctiongethostbyname(0); reportError(0, Severity::style, "obsoleteFunctions"+it->first, it->second);
obsoleteFunctionusleep(0); }
obsoleteFunctionbcmp(0);
obsoleteFunctionbcopy(0);
obsoleteFunctionbzero(0);
obsoleteFunctionecvt(0);
obsoleteFunctionfcvt(0);
obsoleteFunctiongcvt(0);
obsoleteFunctionftime(0);
obsoleteFunctiongetcontext(0);
obsoleteFunctionmakecontext(0);
obsoleteFunctionswapcontext(0);
obsoleteFunctiongetwd(0);
obsoleteFunctionindex(0);
obsoleteFunctionpthread_attr_getstackaddr(0);
obsoleteFunctionpthread_attr_setstackaddr(0);
obsoleteFunctionrindex(0);
obsoleteFunctionscalb(0);
obsoleteFunctionualarm(0);
obsoleteFunctionvfork(0);
obsoleteFunctionwcswcs(0);
} }
std::string name() const std::string name() const
@ -156,30 +115,12 @@ private:
std::string classInfo() const std::string classInfo() const
{ {
return "Warn if any of these obsolete functions are used:\n" std::string info = "Warn if any of these obsolete functions are used:\n";
"* bsd_signal\n" std::list< std::pair<const std::string, const std::string> >::const_iterator it (_obsoleteFunctions.begin()), itend(_obsoleteFunctions.end());
"* gethostbyaddr\n" for(;it!=itend;++it) {
"* gethostbyname\n" info += "* " + it->first + "\n";
"* usleep\n" }
"* bcmp\n" return info;
"* bcopy\n"
"* bzero\n"
"* ecvt\n"
"* fcvt\n"
"* gcvt\n"
"* ftime\n"
"* getcontext\n"
"* makecontext\n"
"* swapcontext\n"
"* getwd\n"
"* index\n"
"* pthread_attr_getstackaddr\n"
"* pthread_attr_setstackaddr\n"
"* rindex\n"
"* scalb\n"
"* ualarm\n"
"* vfork\n"
"* wcswcs\n";
} }
}; };
/// @} /// @}

View File

@ -374,6 +374,66 @@ void CheckOther::checkSelfAssignment()
} }
} }
//---------------------------------------------------------------------------
// if (x)
// return 0;
// else // no need to increase the indentation level
// return 1;
//---------------------------------------------------------------------------
void CheckOther::checkRedundantElseBlock()
{
if (!_settings->_checkCodingStyle)
return;
const char pattern[] = "else| if ( ";
const Token *tok = Token::findmatch(_tokenizer->tokens(), pattern);
while (tok)
{
if (tok->str() == "else")
{
tok = Token::findmatch(tok->tokAt(2), pattern);
continue;
}
tok = tok->next()->link(); // jump over if ( ... )
// search for return in current indentlevel
int indentLevel = 0;
bool returning = false;
for(const Token * iter = tok->tokAt(2); iter != tok->next()->link(); iter = iter->next())
{
if (iter->str() == "{")
indentLevel++;
if (iter->str() == "}")
indentLevel--;
if (indentLevel == 0 && iter->str() == "return")
{
returning = true;
break;
}
}
// skip { .. }
tok = tok->next()->link()->next();
// if it returned, and it's an else if... verify next one
if (returning && tok->str() == "else" && tok->next()->str() == "if")
{
tok = tok->next();
continue;
}
// if its the last else and we're still returning, raise and error
if (returning && tok->str() == "else" && tok->next()->str() != "if")
{
redundantElseBlockError(tok);
}
// next round...
tok = Token::findmatch(tok, pattern);
}
}
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// strtol(str, 0, radix) <- radix must be 0 or 2-36 // strtol(str, 0, radix) <- radix must be 0 or 2-36
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -4267,3 +4327,9 @@ void CheckOther::selfAssignmentError(const Token *tok, const std::string &varnam
reportError(tok, Severity::style, reportError(tok, Severity::style,
"selfAssignment", "Redundant assignment of \"" + varname + "\" to itself"); "selfAssignment", "Redundant assignment of \"" + varname + "\" to itself");
} }
void CheckOther::redundantElseBlockError(const Token *tok)
{
reportError(tok, Severity::style,
"redundantElseBlock", "Redundant \"else {\" encapsulation because the if-statement returns from function");
}

View File

@ -64,6 +64,7 @@ public:
checkOther.sizeofCalculation(); checkOther.sizeofCalculation();
checkOther.checkEmptyCatchBlock(); checkOther.checkEmptyCatchBlock();
checkOther.checkRedundantAssignmentInSwitch(); checkOther.checkRedundantAssignmentInSwitch();
checkOther.checkRedundantElseBlock();
} }
/** @brief Run checks against the simplified token list */ /** @brief Run checks against the simplified token list */
@ -196,6 +197,9 @@ public:
/** @brief %Check for assigning a variable to itself*/ /** @brief %Check for assigning a variable to itself*/
void checkSelfAssignment(); void checkSelfAssignment();
/** @breif %Check for unnecessary else statement */
void checkRedundantElseBlock();
// Error messages.. // Error messages..
void cstyleCastError(const Token *tok); void cstyleCastError(const Token *tok);
void redundantIfDelete0Error(const Token *tok); void redundantIfDelete0Error(const Token *tok);
@ -225,6 +229,7 @@ public:
void emptyCatchBlockError(const Token *tok); void emptyCatchBlockError(const Token *tok);
void redundantAssignmentInSwitchError(const Token *tok, const std::string &varname); void redundantAssignmentInSwitchError(const Token *tok, const std::string &varname);
void selfAssignmentError(const Token *tok, const std::string &varname); void selfAssignmentError(const Token *tok, const std::string &varname);
void redundantElseBlockError(const Token *tok);
void getErrorMessages() void getErrorMessages()
{ {
@ -258,6 +263,7 @@ public:
redundantAssignmentInSwitchError(0, "varname"); redundantAssignmentInSwitchError(0, "varname");
selfAssignmentError(0, "varname"); selfAssignmentError(0, "varname");
invalidScanfError(0); invalidScanfError(0);
redundantElseBlockError(0);
// optimisations // optimisations
postIncrementError(0, "varname", true); postIncrementError(0, "varname", true);
@ -295,6 +301,7 @@ public:
"* unusal pointer arithmetic. For example: \"abc\" + 'd'\n" "* unusal pointer arithmetic. For example: \"abc\" + 'd'\n"
"* empty catch() block\n" "* empty catch() block\n"
"* redundant assignment in a switch statement\n" "* redundant assignment in a switch statement\n"
"* redundant else block after a return\n"
"* look for 'sizeof sizeof ..'\n" "* look for 'sizeof sizeof ..'\n"
"* look for calculations inside sizeof()\n" "* look for calculations inside sizeof()\n"
"* assignment of a variable to itself\n" "* assignment of a variable to itself\n"

View File

@ -107,6 +107,8 @@ private:
TEST_CASE(selfAssignment); TEST_CASE(selfAssignment);
TEST_CASE(testScanf1); TEST_CASE(testScanf1);
TEST_CASE(testScanf2); TEST_CASE(testScanf2);
TEST_CASE(redundantElseBlockTest);
} }
void check(const char code[]) void check(const char code[])
@ -128,6 +130,7 @@ private:
checkOther.sizeofCalculation(); checkOther.sizeofCalculation();
checkOther.checkEmptyCatchBlock(); checkOther.checkEmptyCatchBlock();
checkOther.checkRedundantAssignmentInSwitch(); checkOther.checkRedundantAssignmentInSwitch();
checkOther.checkRedundantElseBlock();
// Simplify token list.. // Simplify token list..
tokenizer.simplifyTokenList(); tokenizer.simplifyTokenList();
@ -3080,6 +3083,72 @@ private:
ASSERT_EQUALS("[test.cpp:6]: (style) scanf without field width limits can crash with huge input data\n" ASSERT_EQUALS("[test.cpp:6]: (style) scanf without field width limits can crash with huge input data\n"
"[test.cpp:7]: (style) scanf without field width limits can crash with huge input data\n", errout.str()); "[test.cpp:7]: (style) scanf without field width limits can crash with huge input data\n", errout.str());
} }
void redundantElseBlockTest()
{
check("void foo()\n"
"{\n"
" if (!true)\n"
" return;\n"
" else {\n"
" dosomething();\n"
" dosomething();\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (style) Redundant \"else {\" encapsulation because the if-statement returns from function\n", errout.str());
check("void foo()\n"
"{\n"
" if (1)\n"
" return;\n"
" else if (2) {\n"
" return;\n"
" } else {\n"
" dosomething();\n"
" dosomething();\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7]: (style) Redundant \"else {\" encapsulation because the if-statement returns from function\n", errout.str());
check("void foo()\n"
"{\n"
" if (1)\n"
" return;\n"
" else if (2) {\n"
" dosomething();\n"
" } else {\n"
" dosomething();\n"
" dosomething();\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void foo()\n"
"{\n"
" if (1)\n"
" dosomething();\n"
" else if (2) {\n"
" return;\n"
" } else {\n"
" dosomething();\n"
" dosomething();\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void foo()\n"
"{\n"
" if (1)\n"
" if (3) return;\n"
" else if (2) {\n"
" return;\n"
" } else {\n"
" dosomething();\n"
" dosomething();\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
}; };
REGISTER_TEST(TestOther) REGISTER_TEST(TestOther)