Refactorized CheckObsoleteFunctions:

- Speedup checkobsoletefunctions by using symboldatabase and by not calling SymbolDatabase::findFunctionByToken() for each function call.
-> Benchmark (SQlite amalgamation):
    CheckObsoleteFunctions::runSimplifiedChecks(): 9s -> 0,016s (562%, 4% on entire runtime)
- Fixed false negative when passing function result as parameter
This commit is contained in:
PKEuS 2012-12-08 02:46:30 -08:00
parent 578e582987
commit 410c0f98d9
2 changed files with 35 additions and 33 deletions

View File

@ -38,42 +38,38 @@ void CheckObsoleteFunctions::obsoleteFunctions()
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { // Functions defined somewhere?
for (unsigned int i = 0; i < symbolDatabase->functionScopes.size(); i++) {
const Scope* scope = symbolDatabase->functionScopes[i];
_obsoleteStandardFunctions.erase(scope->className);
_obsoletePosixFunctions.erase(scope->className);
_obsoleteC99Functions.erase(scope->className);
}
if (tok->isName() && tok->varId()==0 && (tok->next() && tok->next()->str() == "(") && for (unsigned int i = 0; i < symbolDatabase->functionScopes.size(); i++) {
(!Token::Match(tok->previous(), ".|::|:|,") || Token::simpleMatch(tok->tokAt(-2), "std :: gets"))) { const Scope* scope = symbolDatabase->functionScopes[i];
// c function declaration? for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) {
if ((tok->next()->link()->next() && tok->next()->link()->next()->str() == ";") && (tok->previous() && (tok->previous()->str() == "*" || tok->previous()->isName()))) { if (tok->isName() && tok->varId()==0 && (tok->next() && tok->next()->str() == "(") &&
continue; (!Token::Match(tok->previous(), ".|::") || Token::simpleMatch(tok->tokAt(-2), "std ::"))) {
}
// function declaration? std::map<std::string,std::string>::const_iterator it = _obsoleteStandardFunctions.find(tok->str());
const Function * function = symbolDatabase->findFunctionByToken(tok); if (it != _obsoleteStandardFunctions.end()) {
if (function && function->hasBody) { // If checking an old code base it might be uninteresting to update obsolete functions.
_obsoleteStandardFunctions.erase(tok->str()); reportError(tok, Severity::style, "obsoleteFunctions"+it->first, it->second);
_obsoletePosixFunctions.erase(tok->str()); } else {
continue; if (_settings->standards.posix) {
} it = _obsoletePosixFunctions.find(tok->str());
if (it != _obsoletePosixFunctions.end()) {
std::map<std::string,std::string>::const_iterator it = _obsoleteStandardFunctions.find(tok->str()); // If checking an old code base it might be uninteresting to update obsolete functions.
if (it != _obsoleteStandardFunctions.end()) { reportError(tok, Severity::style, "obsoleteFunctions"+it->first, it->second);
// If checking an old code base it might be uninteresting to update obsolete functions. }
// Therefore this is "information"
reportError(tok->next(), Severity::style, "obsoleteFunctions"+it->first, it->second);
} else {
if (_settings->standards.posix) {
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->next(), Severity::style, "obsoleteFunctions"+it->first, it->second);
} }
} if (_settings->standards.c >= Standards::C99) {
if (_settings->standards.c >= Standards::C99) { // alloca : this function is obsolete in C but not in C++ (#4382)
// alloca : this function is obsolete in C but not in C++ (#4382) it = _obsoleteC99Functions.find(tok->str());
it = _obsoleteC99Functions.find(tok->str()); if (it != _obsoleteC99Functions.end() && !(tok->str() == "alloca" && _tokenizer->isCPP())) {
if (it != _obsoleteC99Functions.end() && !(tok->str() == "alloca" && _tokenizer->isCPP())) { reportError(tok, Severity::style, "obsoleteFunctions"+it->first, it->second);
reportError(tok->next(), Severity::style, "obsoleteFunctions"+it->first, it->second); }
} }
} }
} }

View File

@ -214,6 +214,12 @@ private:
" char *x = gets();\n" " char *x = gets();\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Obsolete function 'gets' called. It is recommended to use the function 'fgets' instead.\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (style) Obsolete function 'gets' called. It is recommended to use the function 'fgets' instead.\n", errout.str());
check("void f()\n"
"{\n"
" foo(x, gets());\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Obsolete function 'gets' called. It is recommended to use the function 'fgets' instead.\n", errout.str());
} }
void testalloca() { void testalloca() {