Fixed ticket #3133 (Improve Check: Found obsolete function)

- add check for std::gets
- improve check when multiple obsolete functions are used
- remove false positive (declared functions)
This commit is contained in:
seb777 2011-10-19 20:21:50 +02:00
parent 9257e82475
commit 41d9daa03d
2 changed files with 23 additions and 4 deletions

View File

@ -44,11 +44,16 @@ void CheckObsoleteFunctions::obsoleteFunctions()
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (tok->isName() && tok->varId()==0 && tok->strAt(1) == "(" &&
if (tok->isName() && tok->varId()==0 && (tok->next() && tok->next()->str() == "(") &&
(!Token::Match(tok->previous(), ".|::|:|,") || Token::simpleMatch(tok->previous()->previous(), "std :: gets"))) {
// c function declaration?
if ((tok->next()->link()->next() && tok->next()->link()->next()->str() == ";") && (tok->previous()->str() == "*" || tok->previous()->isName())) {
continue;
}
// function declaration?
if ((tok->previous() && (tok->previous()->str() == "*" || tok->previous()->isName()))
|| symbolDatabase->findFunctionByToken(tok)) {
const Function * function = symbolDatabase->findFunctionByToken(tok);
if (function && function->hasBody) {
_obsoleteStandardFunctions.erase(tok->str());
_obsoletePosixFunctions.erase(tok->str());
continue;

View File

@ -61,6 +61,9 @@ private:
// c declared function
TEST_CASE(test_c_declaration);
// function with body
TEST_CASE(test_function_with_body);
}
void check(const char code[]) {
@ -265,16 +268,27 @@ private:
" char s [ 10 ] ;\n"
" gets ( s ) ;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Found obsolete function 'gets'. It is recommended to use the function 'fgets' instead\n", errout.str());
check("int getcontext(ucontext_t *ucp);\n"
"int f (ucontext_t *ucp)\n"
"{\n"
" getcontext ( ucp ) ;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Found obsolete function 'getcontext'. Due to portability issues with this function, applications are recommended to be rewritten to use POSIX threads\n", errout.str());
}
void test_function_with_body() {
check("char * gets ( char * c ) { return c; }\n"
"int main ()\n"
"{\n"
" char s [ 10 ] ;\n"
" gets ( s ) ;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
};
REGISTER_TEST(TestObsoleteFunctions)