Obsolete functions: Split up functions into posix/standard sets. Only check for obsolete posix functions if --enable=posix has been given.

This commit is contained in:
Daniel Marjamäki 2011-08-03 16:10:43 +02:00
parent 447c3c28b4
commit bab441b7e0
3 changed files with 64 additions and 47 deletions

View File

@ -40,18 +40,35 @@ void CheckObsoleteFunctions::obsoleteFunctions()
if (_tokenizer->isJavaOrCSharp())
return;
const bool checkPosix = _settings->isEnabled("posix");
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
std::list< std::pair<const std::string, const std::string> >::const_iterator it(_obsoleteFunctions.begin()), itend(_obsoleteFunctions.end());
for (; it!=itend; ++it)
if (tok->isName() && tok->varId()==0 && tok->strAt(1) == "(" && !Token::Match(tok->previous(), ".|::|:|,"))
{
if (tok->strAt(1) == it->first && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && !Token::Match(tok, ".|::|:|,"))
// function declaration?
if (tok->previous() && tok->previous()->isName())
continue;
std::map<std::string,std::string>::const_iterator it = _obsoleteStandardFunctions.find(tok->str());
if (it != _obsoleteStandardFunctions.end())
{
// If checking an old code base it might be uninteresting to update obsolete functions.
// Therefore this is "style"
reportError(tok->tokAt(1), Severity::style, "obsoleteFunctions"+it->first, it->second);
// Therefore this is "information"
reportError(tok->tokAt(1), Severity::information, "obsoleteFunctions"+it->first, it->second);
break;
}
else if (checkPosix)
{
it = _obsoletePosixFunctions.find(tok->str());
if (it != _obsoletePosixFunctions.end())
{
// If checking an old code base it might be uninteresting to update obsolete functions.
// Therefore this is "information"
reportError(tok->tokAt(1), Severity::information, "obsoleteFunctions"+it->first, it->second);
break;
}
}
}
}
}

View File

@ -61,56 +61,56 @@ public:
private:
/* function name / error message */
std::list< std::pair< const std::string, const std::string> > _obsoleteFunctions;
std::map<std::string, std::string> _obsoleteStandardFunctions;
std::map<std::string, std::string> _obsoletePosixFunctions;
/** init obsolete functions list ' */
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"));
_obsoletePosixFunctions["bsd_signal"] = "Found obsolete function 'bsd_signal'. It is recommended that new applications use the 'sigaction' function";
_obsoleteFunctions.push_back(std::make_pair("gethostbyaddr","Found obsolete function 'gethostbyaddr'. It is recommended that new applications use the 'getnameinfo' function"));
_obsoleteFunctions.push_back(std::make_pair("gethostbyname","Found obsolete function 'gethostbyname'. It is recommended that new applications use the 'getaddrinfo' function"));
_obsoletePosixFunctions["gethostbyaddr"] = "Found obsolete function 'gethostbyaddr'. It is recommended that new applications use the 'getnameinfo' function";
_obsoletePosixFunctions["gethostbyname"] = "Found obsolete function 'gethostbyname'. It is recommended that new applications use the 'getaddrinfo' function";
_obsoleteFunctions.push_back(std::make_pair("usleep","Found obsolete function 'usleep'. It is recommended that new applications use the 'nanosleep' or 'setitimer' function\n"
"Found obsolete function 'usleep'. POSIX.1-2001 declares usleep() function obsolete and POSIX.1-2008 removes it. It is recommended that new applications use the 'nanosleep' or 'setitimer' function."));
_obsoletePosixFunctions["usleep"] = "Found obsolete function 'usleep'. It is recommended that new applications use the 'nanosleep' or 'setitimer' function\n"
"Found obsolete function 'usleep'. POSIX.1-2001 declares usleep() function obsolete and POSIX.1-2008 removes it. It is recommended that new applications use the 'nanosleep' or 'setitimer' function.";
_obsoleteFunctions.push_back(std::make_pair("bcmp","Found obsolete function 'bcmp'. It is recommended that new applications use the 'memcmp' function"));
_obsoleteFunctions.push_back(std::make_pair("bcopy","Found obsolete function 'bcopy'. It is recommended that new applications use the 'memmove' or 'memcpy' functions"));
_obsoleteFunctions.push_back(std::make_pair("bzero","Found obsolete function 'bzero'. It is recommended that new applications use the 'memset' function"));
_obsoletePosixFunctions["bcmp"]="Found obsolete function 'bcmp'. It is recommended that new applications use the 'memcmp' function";
_obsoletePosixFunctions["bcopy"]="Found obsolete function 'bcopy'. It is recommended that new applications use the 'memmove' or 'memcpy' functions";
_obsoletePosixFunctions["bzero"]="Found obsolete function 'bzero'. It is recommended that new applications use the 'memset' function";
_obsoleteFunctions.push_back(std::make_pair("ecvt","Found obsolete function 'ecvt'. It is recommended that new applications use the 'sprintf' function"));
_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"));
_obsoletePosixFunctions["ecvt"]="Found obsolete function 'ecvt'. It is recommended that new applications use the 'sprintf' function";
_obsoletePosixFunctions["fcvt"]="Found obsolete function 'fcvt'. It is recommended that new applications use the 'sprintf' function";
_obsoletePosixFunctions["gcvt"]="Found obsolete function 'gcvt'. It is recommended that new applications use the 'sprintf' function";
_obsoleteFunctions.push_back(std::make_pair("ftime","Found obsolete function 'ftime'.\n"
_obsoletePosixFunctions["ftime"]="Found obsolete function 'ftime'.\n"
"It is recommended that new applications use time(), gettimeofday(), or clock_gettime() instead. "
"For high-resolution timing on Windows, QueryPerformanceCounter() and QueryPerformanceFrequency may be used."));
"For high-resolution timing on Windows, QueryPerformanceCounter() and QueryPerformanceFrequency may be used.";
_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"));
_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"));
_obsoletePosixFunctions["getcontext"] = "Found obsolete function 'getcontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads";
_obsoletePosixFunctions["makecontext"] = "Found obsolete function 'makecontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads";
_obsoletePosixFunctions["swapcontext"] = "Found obsolete function 'swapcontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads";
_obsoleteFunctions.push_back(std::make_pair("getwd","Found obsolete function 'getwd'. It is recommended that new applications use the 'getcwd' function"));
_obsoletePosixFunctions["getwd"] = "Found obsolete function 'getwd'. It is recommended that new applications use the 'getcwd' function";
/* Disabled to fix #2334
_obsoleteFunctions.push_back(std::make_pair("index","Found obsolete function 'index'. It is recommended to use the function 'strchr' instead"));
*/
// See #2334 (using the Qt Model/View function 'index')
_obsoletePosixFunctions["index"] ="Found obsolete function 'index'. It is recommended to use the function 'strchr' instead";
_obsoleteFunctions.push_back(std::make_pair("rindex","Found obsolete function 'rindex'. It is recommended to use the function 'strrchr' instead"));
_obsoletePosixFunctions["rindex"] = "Found obsolete function 'rindex'. It is recommended to use the function 'strrchr' instead";
_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"));
_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"));
_obsoletePosixFunctions["pthread_attr_getstackaddr"] = "Found obsolete function 'pthread_attr_getstackaddr'.It is recommended that new applications use the 'pthread_attr_getstack' function";
_obsoletePosixFunctions["pthread_attr_setstackaddr"] = "Found obsolete function 'pthread_attr_setstackaddr'.It is recommended that new applications use the 'pthread_attr_setstack' function";
_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"));
_obsoletePosixFunctions["scalbln"] = "Found obsolete function 'scalb'.It is recommended to use either 'scalbln', 'scalblnf' or 'scalblnl' instead of this function";
_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"));
_obsoletePosixFunctions["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";
_obsoleteFunctions.push_back(std::make_pair("vfork","Found obsolete function 'vfork'. It is recommended to use the function 'fork' instead"));
_obsoletePosixFunctions["vfork"] = "Found obsolete function 'vfork'. It is recommended to use the function 'fork' instead";
_obsoleteFunctions.push_back(std::make_pair("wcswcs","Found obsolete function 'wcswcs'. It is recommended to use the function 'wcsstr' instead"));
_obsoletePosixFunctions["wcswcs"] = "Found obsolete function 'wcswcs'. It is recommended to use the function 'wcsstr' instead";
_obsoleteFunctions.push_back(std::make_pair("gets","Found obsolete function 'gets'. It is recommended to use the function 'fgets' instead\n"
"Found obsolete function 'gets'. With gets you'll get buffer overruns if the input data too big for the buffer. It is recommended to use the function 'fgets' instead."));
_obsoleteStandardFunctions["gets"] = "Found obsolete function 'gets'. It is recommended to use the function 'fgets' instead\n"
"Found obsolete function 'gets'. With gets you'll get buffer overruns if the input data too big for the buffer. It is recommended to use the function 'fgets' instead.";
}
@ -118,7 +118,7 @@ private:
{
CheckObsoleteFunctions c(0, settings, errorLogger);
std::list< std::pair<const std::string, const std::string> >::const_iterator it(_obsoleteFunctions.begin()), itend(_obsoleteFunctions.end());
std::map<std::string,std::string>::const_iterator it(_obsoletePosixFunctions.begin()), itend(_obsoletePosixFunctions.end());
for (; it!=itend; ++it)
{
c.reportError(0, Severity::style, "obsoleteFunctions"+it->first, it->second);
@ -133,7 +133,7 @@ private:
std::string classInfo() const
{
std::string info = "Warn if any of these obsolete functions are used:\n";
std::list< std::pair<const std::string, const std::string> >::const_iterator it(_obsoleteFunctions.begin()), itend(_obsoleteFunctions.end());
std::map<std::string,std::string>::const_iterator it(_obsoletePosixFunctions.begin()), itend(_obsoletePosixFunctions.end());
for (; it!=itend; ++it)
{
info += "* " + it->first + "\n";

View File

@ -58,7 +58,7 @@ private:
Settings settings;
settings._checkCodingStyle = true;
settings.inconclusive = true;
settings.addEnabled("posix");
// Tokenize..
Tokenizer tokenizer(&settings, this);
@ -83,7 +83,7 @@ private:
"{\n"
" bsd_signal(SIGABRT, SIG_IGN);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Found obsolete function 'bsd_signal'. It is recommended that new applications use the 'sigaction' function\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (information) Found obsolete function 'bsd_signal'. It is recommended that new applications use the 'sigaction' function\n", errout.str());
check("int f()\n"
"{\n"
@ -103,7 +103,7 @@ private:
" exit(1);\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Found obsolete function 'gethostbyname'. It is recommended that new applications use the 'getaddrinfo' function\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (information) Found obsolete function 'gethostbyname'. It is recommended that new applications use the 'getaddrinfo' function\n", errout.str());
}
void testgethostbyaddr()
@ -116,7 +116,7 @@ private:
" exit(1);\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (style) Found obsolete function 'gethostbyaddr'. It is recommended that new applications use the 'getnameinfo' function\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (information) Found obsolete function 'gethostbyaddr'. It is recommended that new applications use the 'getnameinfo' function\n", errout.str());
}
void testusleep()
@ -125,7 +125,7 @@ private:
"{\n"
" usleep( 1000 );\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Found obsolete function 'usleep'. It is recommended that new applications use the 'nanosleep' or 'setitimer' function\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (information) Found obsolete function 'usleep'. It is recommended that new applications use the 'nanosleep' or 'setitimer' function\n", errout.str());
}
void testindex()
@ -166,8 +166,8 @@ private:
" const char i = index(var, 0);\n"
" return i;\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:4]: (style) Found obsolete function 'index'. It is recommended to use the function 'strchr' instead\n",
"", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (information) Found obsolete function 'index'. It is recommended to use the function 'strchr' instead\n",
errout.str());
}
void test_qt_index()
@ -175,7 +175,7 @@ private:
check("void TDataModel::forceRowRefresh(int row) {\n"
" emit dataChanged(index(row, 0), index(row, columnCount() - 1));\n"
"}\n");
ASSERT_EQUALS("", errout.str());
ASSERT_EQUALS("[test.cpp:2]: (information) Found obsolete function 'index'. It is recommended to use the function 'strchr' instead\n", errout.str());
}
void testrindex()
@ -191,7 +191,7 @@ private:
" const char var[7] = 'rindex';\n"
" print(rindex(var, 0));\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Found obsolete function 'rindex'. It is recommended to use the function 'strrchr' instead\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (information) Found obsolete function 'rindex'. It is recommended to use the function 'strrchr' instead\n", errout.str());
}
@ -211,7 +211,7 @@ private:
"{\n"
" char *x = gets();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Found obsolete function 'gets'. It is recommended to use the function 'fgets' instead\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (information) Found obsolete function 'gets'. It is recommended to use the function 'fgets' instead\n", errout.str());
}