Fixed #4429 (unused functions: handle function declarations better)

This commit is contained in:
Daniel Marjamäki 2013-01-15 17:00:28 +01:00
parent 9f9c654621
commit 2fa35a6f8d
2 changed files with 27 additions and 5 deletions

View File

@ -67,10 +67,8 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer)
tok = funcname->linkAt(1);
// Check that ") {" is found..
if (! Token::simpleMatch(tok, ") {") &&
! Token::simpleMatch(tok, ") const {") &&
! Token::simpleMatch(tok, ") const throw ( ) {") &&
! Token::simpleMatch(tok, ") throw ( ) {"))
if (! Token::Match(tok, ") const| {") &&
! Token::simpleMatch(tok, ") const| throw ( ) {"))
funcname = 0;
if (funcname) {
@ -92,7 +90,23 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer)
}
// Function usage..
const Token *scopeEnd = NULL;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) {
if (scopeEnd == NULL) {
if (tok->str() != ")")
continue;
if (!Token::Match(tok, ") const| {"))
continue;
scopeEnd = tok;
while (scopeEnd->str() != "{")
scopeEnd = scopeEnd->next();
scopeEnd = scopeEnd->link();
} else if (tok == scopeEnd) {
scopeEnd = NULL;
continue;
}
const Token *funcname = 0;
if (Token::Match(tok->next(), "%var% (")) {
@ -117,7 +131,7 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer)
// funcname ( => Assert that the end parenthesis isn't followed by {
if (Token::Match(funcname, "%var% (")) {
if (Token::Match(funcname->linkAt(1), ") const|{"))
if (Token::Match(funcname->linkAt(1), ") const|throw|{"))
funcname = NULL;
}

View File

@ -51,6 +51,8 @@ private:
TEST_CASE(multipleFiles); // same function name in multiple files
TEST_CASE(lineNumber); // Ticket 3059
TEST_CASE(ignore_declaration); // ignore declaration
}
void check(const char code[]) {
@ -257,6 +259,12 @@ private:
ASSERT_EQUALS("[test.cpp:2]: (style) The function 'bar' is never used.\n"
"[test.cpp:1]: (style) The function 'foo' is never used.\n", errout.str());
}
void ignore_declaration() {
check("void f();\n"
"void f() {}");
ASSERT_EQUALS("[test.cpp:2]: (style) The function 'f' is never used.\n", errout.str());
}
};
REGISTER_TEST(TestUnusedFunctions)