Refactorized detection of function usage in CheckUnusedFunction, fixed #5358

This commit is contained in:
PKEuS 2014-03-26 09:12:41 +01:00
parent 02f38772cc
commit 848d079c71
2 changed files with 17 additions and 17 deletions

View File

@ -68,7 +68,6 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi
}
// Function usage..
const Token *scopeEnd = nullptr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) {
// parsing of library code to find called functions
@ -163,31 +162,21 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const char Fi
}
}
if (scopeEnd == nullptr) {
if (!Token::Match(tok, ")|= const| {"))
continue;
scopeEnd = tok;
while (scopeEnd->str() != "{")
scopeEnd = scopeEnd->next();
scopeEnd = scopeEnd->link();
} else if (tok == scopeEnd) {
scopeEnd = nullptr;
continue;
}
const Token *funcname = nullptr;
if (Token::Match(tok->next(), "%var% (")) {
if (tok->scope()->isExecutable() && Token::Match(tok->next(), "%var% (")) {
funcname = tok->next();
}
else if (Token::Match(tok->next(), "%var% <") && Token::simpleMatch(tok->linkAt(2), "> (")) {
else if (tok->scope()->isExecutable() && Token::Match(tok->next(), "%var% <") && Token::simpleMatch(tok->linkAt(2), "> (")) {
funcname = tok->next();
}
else if (Token::Match(tok, "[;{}.,()[=+-/&|!?:] %var% [(),;:}]"))
else if (Token::Match(tok, "[;{}.,()[=+-/|!?:] &| %var% [(),;:}]")) {
funcname = tok->next();
if (tok->str() == "&")
funcname = funcname->next();
}
else if (Token::Match(tok, "[=(,] &| %var% :: %var%")) {
funcname = tok->next();

View File

@ -49,6 +49,7 @@ private:
TEST_CASE(operator1); // #3195
TEST_CASE(returnRef);
TEST_CASE(attribute); // #3471 - FP __attribute__(constructor)
TEST_CASE(initializer_list);
TEST_CASE(multipleFiles); // same function name in multiple files
@ -284,6 +285,16 @@ private:
ASSERT_EQUALS("", errout.str());
}
void initializer_list() {
check("int foo() { return 0; }\n"
"struct A {\n"
" A() : m_i(foo())\n"
" {}\n"
"int m_i;\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void multipleFiles() {
CheckUnusedFunctions c;