Skip to content

Commit bab441b

Browse files
author
Daniel Marjamäki
committed
Obsolete functions: Split up functions into posix/standard sets. Only check for obsolete posix functions if --enable=posix has been given.
1 parent 447c3c2 commit bab441b

3 files changed

Lines changed: 64 additions & 47 deletions

File tree

lib/checkobsoletefunctions.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,35 @@ void CheckObsoleteFunctions::obsoleteFunctions()
4040
if (_tokenizer->isJavaOrCSharp())
4141
return;
4242

43+
const bool checkPosix = _settings->isEnabled("posix");
44+
4345
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
4446
{
45-
std::list< std::pair<const std::string, const std::string> >::const_iterator it(_obsoleteFunctions.begin()), itend(_obsoleteFunctions.end());
46-
for (; it!=itend; ++it)
47+
if (tok->isName() && tok->varId()==0 && tok->strAt(1) == "(" && !Token::Match(tok->previous(), ".|::|:|,"))
4748
{
48-
if (tok->strAt(1) == it->first && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && !Token::Match(tok, ".|::|:|,"))
49+
// function declaration?
50+
if (tok->previous() && tok->previous()->isName())
51+
continue;
52+
53+
std::map<std::string,std::string>::const_iterator it = _obsoleteStandardFunctions.find(tok->str());
54+
if (it != _obsoleteStandardFunctions.end())
4955
{
5056
// If checking an old code base it might be uninteresting to update obsolete functions.
51-
// Therefore this is "style"
52-
reportError(tok->tokAt(1), Severity::style, "obsoleteFunctions"+it->first, it->second);
57+
// Therefore this is "information"
58+
reportError(tok->tokAt(1), Severity::information, "obsoleteFunctions"+it->first, it->second);
5359
break;
5460
}
61+
else if (checkPosix)
62+
{
63+
it = _obsoletePosixFunctions.find(tok->str());
64+
if (it != _obsoletePosixFunctions.end())
65+
{
66+
// If checking an old code base it might be uninteresting to update obsolete functions.
67+
// Therefore this is "information"
68+
reportError(tok->tokAt(1), Severity::information, "obsoleteFunctions"+it->first, it->second);
69+
break;
70+
}
71+
}
5572
}
5673
}
5774
}

lib/checkobsoletefunctions.h

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -61,64 +61,64 @@ class CheckObsoleteFunctions : public Check
6161

6262
private:
6363
/* function name / error message */
64-
std::list< std::pair< const std::string, const std::string> > _obsoleteFunctions;
64+
std::map<std::string, std::string> _obsoleteStandardFunctions;
65+
std::map<std::string, std::string> _obsoletePosixFunctions;
6566

6667
/** init obsolete functions list ' */
6768
void initObsoleteFunctions()
6869
{
69-
_obsoleteFunctions.push_back(std::make_pair("bsd_signal","Found obsolete function 'bsd_signal'. It is recommended that new applications use the 'sigaction' function"));
70+
_obsoletePosixFunctions["bsd_signal"] = "Found obsolete function 'bsd_signal'. It is recommended that new applications use the 'sigaction' function";
7071

71-
_obsoleteFunctions.push_back(std::make_pair("gethostbyaddr","Found obsolete function 'gethostbyaddr'. It is recommended that new applications use the 'getnameinfo' function"));
72-
_obsoleteFunctions.push_back(std::make_pair("gethostbyname","Found obsolete function 'gethostbyname'. It is recommended that new applications use the 'getaddrinfo' function"));
72+
_obsoletePosixFunctions["gethostbyaddr"] = "Found obsolete function 'gethostbyaddr'. It is recommended that new applications use the 'getnameinfo' function";
73+
_obsoletePosixFunctions["gethostbyname"] = "Found obsolete function 'gethostbyname'. It is recommended that new applications use the 'getaddrinfo' function";
7374

74-
_obsoleteFunctions.push_back(std::make_pair("usleep","Found obsolete function 'usleep'. It is recommended that new applications use the 'nanosleep' or 'setitimer' function\n"
75-
"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."));
75+
_obsoletePosixFunctions["usleep"] = "Found obsolete function 'usleep'. It is recommended that new applications use the 'nanosleep' or 'setitimer' function\n"
76+
"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.";
7677

77-
_obsoleteFunctions.push_back(std::make_pair("bcmp","Found obsolete function 'bcmp'. It is recommended that new applications use the 'memcmp' function"));
78-
_obsoleteFunctions.push_back(std::make_pair("bcopy","Found obsolete function 'bcopy'. It is recommended that new applications use the 'memmove' or 'memcpy' functions"));
79-
_obsoleteFunctions.push_back(std::make_pair("bzero","Found obsolete function 'bzero'. It is recommended that new applications use the 'memset' function"));
78+
_obsoletePosixFunctions["bcmp"]="Found obsolete function 'bcmp'. It is recommended that new applications use the 'memcmp' function";
79+
_obsoletePosixFunctions["bcopy"]="Found obsolete function 'bcopy'. It is recommended that new applications use the 'memmove' or 'memcpy' functions";
80+
_obsoletePosixFunctions["bzero"]="Found obsolete function 'bzero'. It is recommended that new applications use the 'memset' function";
8081

81-
_obsoleteFunctions.push_back(std::make_pair("ecvt","Found obsolete function 'ecvt'. It is recommended that new applications use the 'sprintf' function"));
82-
_obsoleteFunctions.push_back(std::make_pair("fcvt","Found obsolete function 'fcvt'. It is recommended that new applications use the 'sprintf' function"));
83-
_obsoleteFunctions.push_back(std::make_pair("gcvt","Found obsolete function 'gcvt'. It is recommended that new applications use the 'sprintf' function"));
82+
_obsoletePosixFunctions["ecvt"]="Found obsolete function 'ecvt'. It is recommended that new applications use the 'sprintf' function";
83+
_obsoletePosixFunctions["fcvt"]="Found obsolete function 'fcvt'. It is recommended that new applications use the 'sprintf' function";
84+
_obsoletePosixFunctions["gcvt"]="Found obsolete function 'gcvt'. It is recommended that new applications use the 'sprintf' function";
8485

85-
_obsoleteFunctions.push_back(std::make_pair("ftime","Found obsolete function 'ftime'.\n"
86+
_obsoletePosixFunctions["ftime"]="Found obsolete function 'ftime'.\n"
8687
"It is recommended that new applications use time(), gettimeofday(), or clock_gettime() instead. "
87-
"For high-resolution timing on Windows, QueryPerformanceCounter() and QueryPerformanceFrequency may be used."));
88+
"For high-resolution timing on Windows, QueryPerformanceCounter() and QueryPerformanceFrequency may be used.";
8889

89-
_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"));
90-
_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"));
91-
_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"));
90+
_obsoletePosixFunctions["getcontext"] = "Found obsolete function 'getcontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads";
91+
_obsoletePosixFunctions["makecontext"] = "Found obsolete function 'makecontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads";
92+
_obsoletePosixFunctions["swapcontext"] = "Found obsolete function 'swapcontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads";
9293

93-
_obsoleteFunctions.push_back(std::make_pair("getwd","Found obsolete function 'getwd'. It is recommended that new applications use the 'getcwd' function"));
94+
_obsoletePosixFunctions["getwd"] = "Found obsolete function 'getwd'. It is recommended that new applications use the 'getcwd' function";
9495

95-
/* Disabled to fix #2334
96-
_obsoleteFunctions.push_back(std::make_pair("index","Found obsolete function 'index'. It is recommended to use the function 'strchr' instead"));
97-
*/
96+
// See #2334 (using the Qt Model/View function 'index')
97+
_obsoletePosixFunctions["index"] ="Found obsolete function 'index'. It is recommended to use the function 'strchr' instead";
9898

99-
_obsoleteFunctions.push_back(std::make_pair("rindex","Found obsolete function 'rindex'. It is recommended to use the function 'strrchr' instead"));
99+
_obsoletePosixFunctions["rindex"] = "Found obsolete function 'rindex'. It is recommended to use the function 'strrchr' instead";
100100

101-
_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"));
102-
_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"));
101+
_obsoletePosixFunctions["pthread_attr_getstackaddr"] = "Found obsolete function 'pthread_attr_getstackaddr'.It is recommended that new applications use the 'pthread_attr_getstack' function";
102+
_obsoletePosixFunctions["pthread_attr_setstackaddr"] = "Found obsolete function 'pthread_attr_setstackaddr'.It is recommended that new applications use the 'pthread_attr_setstack' function";
103103

104-
_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"));
104+
_obsoletePosixFunctions["scalbln"] = "Found obsolete function 'scalb'.It is recommended to use either 'scalbln', 'scalblnf' or 'scalblnl' instead of this function";
105105

106-
_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"));
106+
_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";
107107

108-
_obsoleteFunctions.push_back(std::make_pair("vfork","Found obsolete function 'vfork'. It is recommended to use the function 'fork' instead"));
108+
_obsoletePosixFunctions["vfork"] = "Found obsolete function 'vfork'. It is recommended to use the function 'fork' instead";
109109

110-
_obsoleteFunctions.push_back(std::make_pair("wcswcs","Found obsolete function 'wcswcs'. It is recommended to use the function 'wcsstr' instead"));
110+
_obsoletePosixFunctions["wcswcs"] = "Found obsolete function 'wcswcs'. It is recommended to use the function 'wcsstr' instead";
111111

112-
_obsoleteFunctions.push_back(std::make_pair("gets","Found obsolete function 'gets'. It is recommended to use the function 'fgets' instead\n"
113-
"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."));
112+
_obsoleteStandardFunctions["gets"] = "Found obsolete function 'gets'. It is recommended to use the function 'fgets' instead\n"
113+
"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.";
114114

115115
}
116116

117117
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
118118
{
119119
CheckObsoleteFunctions c(0, settings, errorLogger);
120120

121-
std::list< std::pair<const std::string, const std::string> >::const_iterator it(_obsoleteFunctions.begin()), itend(_obsoleteFunctions.end());
121+
std::map<std::string,std::string>::const_iterator it(_obsoletePosixFunctions.begin()), itend(_obsoletePosixFunctions.end());
122122
for (; it!=itend; ++it)
123123
{
124124
c.reportError(0, Severity::style, "obsoleteFunctions"+it->first, it->second);
@@ -133,7 +133,7 @@ class CheckObsoleteFunctions : public Check
133133
std::string classInfo() const
134134
{
135135
std::string info = "Warn if any of these obsolete functions are used:\n";
136-
std::list< std::pair<const std::string, const std::string> >::const_iterator it(_obsoleteFunctions.begin()), itend(_obsoleteFunctions.end());
136+
std::map<std::string,std::string>::const_iterator it(_obsoletePosixFunctions.begin()), itend(_obsoletePosixFunctions.end());
137137
for (; it!=itend; ++it)
138138
{
139139
info += "* " + it->first + "\n";

test/testobsoletefunctions.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class TestObsoleteFunctions : public TestFixture
5858

5959
Settings settings;
6060
settings._checkCodingStyle = true;
61-
settings.inconclusive = true;
61+
settings.addEnabled("posix");
6262

6363
// Tokenize..
6464
Tokenizer tokenizer(&settings, this);
@@ -83,7 +83,7 @@ class TestObsoleteFunctions : public TestFixture
8383
"{\n"
8484
" bsd_signal(SIGABRT, SIG_IGN);\n"
8585
"}\n");
86-
ASSERT_EQUALS("[test.cpp:3]: (style) Found obsolete function 'bsd_signal'. It is recommended that new applications use the 'sigaction' function\n", errout.str());
86+
ASSERT_EQUALS("[test.cpp:3]: (information) Found obsolete function 'bsd_signal'. It is recommended that new applications use the 'sigaction' function\n", errout.str());
8787

8888
check("int f()\n"
8989
"{\n"
@@ -103,7 +103,7 @@ class TestObsoleteFunctions : public TestFixture
103103
" exit(1);\n"
104104
" }\n"
105105
"}\n");
106-
ASSERT_EQUALS("[test.cpp:4]: (style) Found obsolete function 'gethostbyname'. It is recommended that new applications use the 'getaddrinfo' function\n", errout.str());
106+
ASSERT_EQUALS("[test.cpp:4]: (information) Found obsolete function 'gethostbyname'. It is recommended that new applications use the 'getaddrinfo' function\n", errout.str());
107107
}
108108

109109
void testgethostbyaddr()
@@ -116,7 +116,7 @@ class TestObsoleteFunctions : public TestFixture
116116
" exit(1);\n"
117117
" }\n"
118118
"}\n");
119-
ASSERT_EQUALS("[test.cpp:5]: (style) Found obsolete function 'gethostbyaddr'. It is recommended that new applications use the 'getnameinfo' function\n", errout.str());
119+
ASSERT_EQUALS("[test.cpp:5]: (information) Found obsolete function 'gethostbyaddr'. It is recommended that new applications use the 'getnameinfo' function\n", errout.str());
120120
}
121121

122122
void testusleep()
@@ -125,7 +125,7 @@ class TestObsoleteFunctions : public TestFixture
125125
"{\n"
126126
" usleep( 1000 );\n"
127127
"}\n");
128-
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());
128+
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());
129129
}
130130

131131
void testindex()
@@ -166,16 +166,16 @@ class TestObsoleteFunctions : public TestFixture
166166
" const char i = index(var, 0);\n"
167167
" return i;\n"
168168
"}\n");
169-
TODO_ASSERT_EQUALS("[test.cpp:4]: (style) Found obsolete function 'index'. It is recommended to use the function 'strchr' instead\n",
170-
"", errout.str());
169+
ASSERT_EQUALS("[test.cpp:4]: (information) Found obsolete function 'index'. It is recommended to use the function 'strchr' instead\n",
170+
errout.str());
171171
}
172172

173173
void test_qt_index()
174174
{
175175
check("void TDataModel::forceRowRefresh(int row) {\n"
176176
" emit dataChanged(index(row, 0), index(row, columnCount() - 1));\n"
177177
"}\n");
178-
ASSERT_EQUALS("", errout.str());
178+
ASSERT_EQUALS("[test.cpp:2]: (information) Found obsolete function 'index'. It is recommended to use the function 'strchr' instead\n", errout.str());
179179
}
180180

181181
void testrindex()
@@ -191,7 +191,7 @@ class TestObsoleteFunctions : public TestFixture
191191
" const char var[7] = 'rindex';\n"
192192
" print(rindex(var, 0));\n"
193193
"}\n");
194-
ASSERT_EQUALS("[test.cpp:4]: (style) Found obsolete function 'rindex'. It is recommended to use the function 'strrchr' instead\n", errout.str());
194+
ASSERT_EQUALS("[test.cpp:4]: (information) Found obsolete function 'rindex'. It is recommended to use the function 'strrchr' instead\n", errout.str());
195195
}
196196

197197

@@ -211,7 +211,7 @@ class TestObsoleteFunctions : public TestFixture
211211
"{\n"
212212
" char *x = gets();\n"
213213
"}\n");
214-
ASSERT_EQUALS("[test.cpp:3]: (style) Found obsolete function 'gets'. It is recommended to use the function 'fgets' instead\n", errout.str());
214+
ASSERT_EQUALS("[test.cpp:3]: (information) Found obsolete function 'gets'. It is recommended to use the function 'fgets' instead\n", errout.str());
215215
}
216216

217217

0 commit comments

Comments
 (0)