Fixed false negative in unused functions check when function returns reference

Removed some unnecessary loops
This commit is contained in:
PKEuS 2012-04-09 11:00:31 +02:00
parent b8d233f1a2
commit c12d82aeb9
2 changed files with 18 additions and 26 deletions

View File

@ -60,26 +60,23 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer)
if (Token::Match(tok, "%type% %var% (")) if (Token::Match(tok, "%type% %var% ("))
funcname = tok->next(); funcname = tok->next();
else if (Token::Match(tok, "%type% * %var% (")) else if (Token::Match(tok, "%type% *|& %var% ("))
funcname = tok->tokAt(2); funcname = tok->tokAt(2);
else if (Token::Match(tok, "%type% :: %var% (") && !Token::Match(tok, tok->strAt(2).c_str())) else if (Token::Match(tok, "%type% :: %var% (") && !Token::Match(tok, tok->strAt(2).c_str()))
funcname = tok->tokAt(2); funcname = tok->tokAt(2);
// Don't assume throw as a function name: void foo() throw () {} // Don't assume throw as a function name: void foo() throw () {}
if (Token::Match(tok->previous(), ")|const")) if (Token::Match(tok->previous(), ")|const") || funcname == 0)
funcname = 0; continue;
tok = funcname->linkAt(1);
// Check that ") {" is found.. // Check that ") {" is found..
for (const Token *tok2 = funcname; tok2; tok2 = tok2->next()) { if (! Token::simpleMatch(tok, ") {") &&
if (tok2->str() == ")") { ! Token::simpleMatch(tok, ") const {") &&
if (! Token::simpleMatch(tok2, ") {") && ! Token::simpleMatch(tok, ") const throw ( ) {") &&
! Token::simpleMatch(tok2, ") const {") && ! Token::simpleMatch(tok, ") throw ( ) {"))
! Token::simpleMatch(tok2, ") const throw ( ) {") && funcname = 0;
! Token::simpleMatch(tok2, ") throw ( ) {"))
funcname = 0;
break;
}
}
if (funcname) { if (funcname) {
FunctionUsage &func = _functions[ funcname->str()]; FunctionUsage &func = _functions[ funcname->str()];
@ -118,19 +115,8 @@ 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% (")) {
int parlevel = 0; if (Token::Match(funcname->linkAt(1), ") const|{"))
for (const Token *tok2 = funcname; tok2; tok2 = tok2->next()) { funcname = NULL;
if (tok2->str() == "(")
++parlevel;
else if (tok2->str() == ")") {
--parlevel;
if (parlevel == 0 && (Token::Match(tok2, ") const|{")))
funcname = NULL;
if (parlevel <= 0)
break;
}
}
} }
if (funcname) { if (funcname) {

View File

@ -46,6 +46,7 @@ private:
TEST_CASE(unusedMain); TEST_CASE(unusedMain);
TEST_CASE(initializationIsNotAFunction); TEST_CASE(initializationIsNotAFunction);
TEST_CASE(operator1); // #3195 TEST_CASE(operator1); // #3195
TEST_CASE(returnRef);
TEST_CASE(multipleFiles); // same function name in multiple files TEST_CASE(multipleFiles); // same function name in multiple files
@ -203,6 +204,11 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void returnRef() {
check("int& foo() {return x;}");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used\n", errout.str());
}
void multipleFiles() { void multipleFiles() {
CheckUnusedFunctions c; CheckUnusedFunctions c;