Fixed #4429 (unused functions: handle function declarations better)
This commit is contained in:
parent
9f9c654621
commit
2fa35a6f8d
|
@ -67,10 +67,8 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer)
|
||||||
tok = funcname->linkAt(1);
|
tok = funcname->linkAt(1);
|
||||||
|
|
||||||
// Check that ") {" is found..
|
// Check that ") {" is found..
|
||||||
if (! Token::simpleMatch(tok, ") {") &&
|
if (! Token::Match(tok, ") const| {") &&
|
||||||
! Token::simpleMatch(tok, ") const {") &&
|
! Token::simpleMatch(tok, ") const| throw ( ) {"))
|
||||||
! Token::simpleMatch(tok, ") const throw ( ) {") &&
|
|
||||||
! Token::simpleMatch(tok, ") throw ( ) {"))
|
|
||||||
funcname = 0;
|
funcname = 0;
|
||||||
|
|
||||||
if (funcname) {
|
if (funcname) {
|
||||||
|
@ -92,7 +90,23 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function usage..
|
// Function usage..
|
||||||
|
const Token *scopeEnd = NULL;
|
||||||
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) {
|
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;
|
const Token *funcname = 0;
|
||||||
|
|
||||||
if (Token::Match(tok->next(), "%var% (")) {
|
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 {
|
// funcname ( => Assert that the end parenthesis isn't followed by {
|
||||||
if (Token::Match(funcname, "%var% (")) {
|
if (Token::Match(funcname, "%var% (")) {
|
||||||
if (Token::Match(funcname->linkAt(1), ") const|{"))
|
if (Token::Match(funcname->linkAt(1), ") const|throw|{"))
|
||||||
funcname = NULL;
|
funcname = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -51,6 +51,8 @@ private:
|
||||||
TEST_CASE(multipleFiles); // same function name in multiple files
|
TEST_CASE(multipleFiles); // same function name in multiple files
|
||||||
|
|
||||||
TEST_CASE(lineNumber); // Ticket 3059
|
TEST_CASE(lineNumber); // Ticket 3059
|
||||||
|
|
||||||
|
TEST_CASE(ignore_declaration); // ignore declaration
|
||||||
}
|
}
|
||||||
|
|
||||||
void check(const char code[]) {
|
void check(const char code[]) {
|
||||||
|
@ -257,6 +259,12 @@ private:
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (style) The function 'bar' is never used.\n"
|
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());
|
"[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)
|
REGISTER_TEST(TestUnusedFunctions)
|
||||||
|
|
Loading…
Reference in New Issue