From 2fa35a6f8debd353eccba4d473de00d1635fb37d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 15 Jan 2013 17:00:28 +0100 Subject: [PATCH] Fixed #4429 (unused functions: handle function declarations better) --- lib/checkunusedfunctions.cpp | 24 +++++++++++++++++++----- test/testunusedfunctions.cpp | 8 ++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index 515f5688c..aab4ff90a 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -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; } diff --git a/test/testunusedfunctions.cpp b/test/testunusedfunctions.cpp index 281703296..27b7811e8 100644 --- a/test/testunusedfunctions.cpp +++ b/test/testunusedfunctions.cpp @@ -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)