Skip to content

Commit 7398453

Browse files
seb777Daniel Marjamäki
authored andcommitted
Refactoring of obsolete functions checks. Ticket: cppcheck-opensource#1940
1 parent 8d52c33 commit 7398453

5 files changed

Lines changed: 194 additions & 312 deletions

File tree

lib/checkobsoletefunctions.cpp

Lines changed: 7 additions & 208 deletions
Original file line numberDiff line numberDiff line change
@@ -38,216 +38,15 @@ void CheckObsoleteFunctions::obsoleteFunctions()
3838

3939
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
4040
{
41-
if (tok->strAt(1) == "bsd_signal" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
42-
{
43-
obsoleteFunctionbsd_signal(tok->tokAt(1));
44-
}
45-
else if (tok->strAt(1) == "gethostbyaddr" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
46-
{
47-
obsoleteFunctiongethostbyaddr(tok->tokAt(1));
48-
}
49-
else if (tok->strAt(1) == "gethostbyname" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
50-
{
51-
obsoleteFunctiongethostbyname(tok->tokAt(1));
52-
}
53-
else if (tok->strAt(1) == "usleep" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
54-
{
55-
obsoleteFunctionusleep(tok->tokAt(1));
56-
}
57-
else if (tok->strAt(1) == "bcmp" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
58-
{
59-
obsoleteFunctionbcmp(tok->tokAt(1));
60-
}
61-
else if (tok->strAt(1) == "bcopy" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
62-
{
63-
obsoleteFunctionbcopy(tok->tokAt(1));
64-
}
65-
else if (tok->strAt(1) == "bzero" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
66-
{
67-
obsoleteFunctionbzero(tok->tokAt(1));
68-
}
69-
else if (tok->strAt(1) == "ecvt" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
70-
{
71-
obsoleteFunctionecvt(tok->tokAt(1));
72-
}
73-
else if (tok->strAt(1) == "fcvt" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
74-
{
75-
obsoleteFunctionfcvt(tok->tokAt(1));
76-
}
77-
else if (tok->strAt(1) == "gcvt" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
78-
{
79-
obsoleteFunctiongcvt(tok->tokAt(1));
80-
}
81-
else if (tok->strAt(1) == "ftime" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
82-
{
83-
obsoleteFunctionftime(tok->tokAt(1));
84-
}
85-
else if (tok->strAt(1) == "getcontext" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
86-
{
87-
obsoleteFunctiongetcontext(tok->tokAt(1));
88-
}
89-
else if (tok->strAt(1) == "makecontext" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
90-
{
91-
obsoleteFunctionmakecontext(tok->tokAt(1));
92-
}
93-
else if (tok->strAt(1) == "swapcontext" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
94-
{
95-
obsoleteFunctionswapcontext(tok->tokAt(1));
96-
}
97-
else if (tok->strAt(1) == "getwd" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
98-
{
99-
obsoleteFunctiongetwd(tok->tokAt(1));
100-
}
101-
else if (tok->strAt(1) == "index" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
102-
{
103-
obsoleteFunctionindex(tok->tokAt(1));
104-
}
105-
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) != "::")
106-
{
107-
obsoleteFunctionpthread_attr_getstackaddr(tok->tokAt(1));
108-
}
109-
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) != "::")
110-
{
111-
obsoleteFunctionpthread_attr_setstackaddr(tok->tokAt(1));
112-
}
113-
else if (tok->strAt(1) == "rindex" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
114-
{
115-
obsoleteFunctionrindex(tok->tokAt(1));
116-
}
117-
else if (tok->strAt(1) == "scalb" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
118-
{
119-
obsoleteFunctionscalb(tok->tokAt(1));
120-
}
121-
else if (tok->strAt(1) == "ualarm" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
122-
{
123-
obsoleteFunctionualarm(tok->tokAt(1));
124-
}
125-
else if (tok->strAt(1) == "vfork" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
126-
{
127-
obsoleteFunctionvfork(tok->tokAt(1));
128-
}
129-
else if (tok->strAt(1) == "wcswcs" && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
130-
{
131-
obsoleteFunctionwcswcs(tok->tokAt(1));
41+
std::list< std::pair<const std::string, const std::string> >::const_iterator it (_obsoleteFunctions.begin()), itend(_obsoleteFunctions.end());
42+
for(;it!=itend;++it) {
43+
if (tok->strAt(1) == it->first && tok->strAt(2) == "(" && tok->tokAt(1)->varId() == 0 && !tok->tokAt(0)->isName() && tok->strAt(0) != "." && tok->strAt(0) != "::")
44+
{
45+
reportError(tok->tokAt(1), Severity::style, "obsoleteFunctions"+it->first, it->second);
46+
break;
47+
}
13248
}
13349
}
13450
}
13551
//---------------------------------------------------------------------------
13652

137-
138-
void CheckObsoleteFunctions::obsoleteFunctionbsd_signal(const Token *tok)
139-
{
140-
reportError(tok, Severity::style, "obsoleteFunctionbsd_signal", "Found obsolete function 'bsd_signal'. It is recommended that new applications use the 'sigaction' function");
141-
}
142-
143-
void CheckObsoleteFunctions::obsoleteFunctiongethostbyaddr(const Token *tok)
144-
{
145-
reportError(tok, Severity::style, "obsoleteFunctiongethostbyaddr", "Found obsolete function 'gethostbyaddr'. It is recommended that new applications use the 'getaddrinfo' function");
146-
}
147-
148-
void CheckObsoleteFunctions::obsoleteFunctiongethostbyname(const Token *tok)
149-
{
150-
reportError(tok, Severity::style, "obsoleteFunctiongethostbyname", "Found obsolete function 'gethostbyname'. It is recommended that new applications use the 'getnameinfo' function");
151-
}
152-
153-
void CheckObsoleteFunctions::obsoleteFunctionusleep(const Token *tok)
154-
{
155-
reportError(tok, Severity::style, "obsoleteFunctionusleep", "Found obsolete function 'usleep'. It is recommended that new applications use the 'nanosleep' or 'setitimer' function");
156-
}
157-
158-
void CheckObsoleteFunctions::obsoleteFunctionbcmp(const Token *tok)
159-
{
160-
reportError(tok, Severity::style, "obsoleteFunctionbcmp", "Found obsolete function 'bcmp'. It is recommended that new applications use the 'memcmp' function");
161-
}
162-
163-
void CheckObsoleteFunctions::obsoleteFunctionbcopy(const Token *tok)
164-
{
165-
reportError(tok, Severity::style, "obsoleteFunctionbcopy", "Found obsolete function 'bcopy'. It is recommended that new applications use the 'memmove' function");
166-
}
167-
168-
void CheckObsoleteFunctions::obsoleteFunctionbzero(const Token *tok)
169-
{
170-
reportError(tok, Severity::style, "obsoleteFunctionbzero", "Found obsolete function 'bzero'. It is recommended that new applications use the 'memset' function");
171-
}
172-
173-
void CheckObsoleteFunctions::obsoleteFunctionecvt(const Token *tok)
174-
{
175-
reportError(tok, Severity::style, "obsoleteFunctionecvt", "Found obsolete function 'ecvt'. It is recommended that new applications use the 'sprintf' function");
176-
}
177-
178-
void CheckObsoleteFunctions::obsoleteFunctionfcvt(const Token *tok)
179-
{
180-
reportError(tok, Severity::style, "obsoleteFunctionfcvt", "Found obsolete function 'fcvt'. It is recommended that new applications use the 'sprintf' function");
181-
}
182-
183-
void CheckObsoleteFunctions::obsoleteFunctiongcvt(const Token *tok)
184-
{
185-
reportError(tok, Severity::style, "obsoleteFunctiongcvt", "Found obsolete function 'gcvt'. It is recommended that new applications use the 'sprintf' function");
186-
}
187-
188-
void CheckObsoleteFunctions::obsoleteFunctionftime(const Token *tok)
189-
{
190-
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");
191-
}
192-
193-
void CheckObsoleteFunctions::obsoleteFunctiongetcontext(const Token *tok)
194-
{
195-
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");
196-
}
197-
198-
void CheckObsoleteFunctions::obsoleteFunctionmakecontext(const Token *tok)
199-
{
200-
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");
201-
}
202-
203-
void CheckObsoleteFunctions::obsoleteFunctionswapcontext(const Token *tok)
204-
{
205-
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");
206-
}
207-
208-
void CheckObsoleteFunctions::obsoleteFunctiongetwd(const Token *tok)
209-
{
210-
reportError(tok, Severity::style, "obsoleteFunctiongetwd", "Found obsolete function 'getwd'. It is recommended that new applications use the 'getcwd' function");
211-
}
212-
213-
void CheckObsoleteFunctions::obsoleteFunctionindex(const Token *tok)
214-
{
215-
reportError(tok, Severity::style, "obsoleteFunctionindex", "Found obsolete function 'index'. It is recommended to use the function 'strchr' instead");
216-
}
217-
218-
void CheckObsoleteFunctions::obsoleteFunctionpthread_attr_getstackaddr(const Token *tok)
219-
{
220-
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");
221-
}
222-
223-
void CheckObsoleteFunctions::obsoleteFunctionpthread_attr_setstackaddr(const Token *tok)
224-
{
225-
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");
226-
}
227-
228-
void CheckObsoleteFunctions::obsoleteFunctionrindex(const Token *tok)
229-
{
230-
reportError(tok, Severity::style, "obsoleteFunctionrindex", "Found obsolete function 'rindex'. It is recommended to use the function 'strrchr' instead");
231-
}
232-
233-
void CheckObsoleteFunctions::obsoleteFunctionscalb(const Token *tok)
234-
{
235-
reportError(tok, Severity::style, "obsoleteFunctionscalb", "Found obsolete function 'scalb'.It is recommended to use either 'scalbln', 'scalblnf()' or 'scalblnl' instead of this function");
236-
}
237-
238-
void CheckObsoleteFunctions::obsoleteFunctionualarm(const Token *tok)
239-
{
240-
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");
241-
}
242-
243-
void CheckObsoleteFunctions::obsoleteFunctionvfork(const Token *tok)
244-
{
245-
reportError(tok, Severity::style, "obsoleteFunctionvfork", "Found obsolete function 'vfork'. It is recommended to use the function 'fork' instead");
246-
}
247-
248-
void CheckObsoleteFunctions::obsoleteFunctionwcswcs(const Token *tok)
249-
{
250-
reportError(tok, Severity::style, "obsoleteFunctionwcswcs", "Found obsolete function 'wcswcs'. It is recommended to use the function 'wcsstr' instead");
251-
}
252-
253-

0 commit comments

Comments
 (0)