From 41d9daa03d1a8cbb4b39c7c49685bf040509d516 Mon Sep 17 00:00:00 2001 From: seb777 Date: Wed, 19 Oct 2011 20:21:50 +0200 Subject: [PATCH] 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) --- lib/checkobsoletefunctions.cpp | 11 ++++++++--- test/testobsoletefunctions.cpp | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/checkobsoletefunctions.cpp b/lib/checkobsoletefunctions.cpp index 38a48ae05..e0e9cfe1a 100644 --- a/lib/checkobsoletefunctions.cpp +++ b/lib/checkobsoletefunctions.cpp @@ -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; diff --git a/test/testobsoletefunctions.cpp b/test/testobsoletefunctions.cpp index 30763a4e2..a755a2f4a 100644 --- a/test/testobsoletefunctions.cpp +++ b/test/testobsoletefunctions.cpp @@ -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)