Support function pointers in CheckUnusedVar::checkFunctionVariableUsage_iterateScopes() (#7194)

This commit is contained in:
PKEuS 2016-01-30 20:59:32 +01:00
parent b908bb18a9
commit 3b046b42a6
2 changed files with 27 additions and 0 deletions

View File

@ -704,6 +704,8 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
if (type == Variables::none || isPartOfClassStructUnion(i->typeStartToken())) if (type == Variables::none || isPartOfClassStructUnion(i->typeStartToken()))
continue; continue;
const Token* defValTok = i->nameToken()->next(); const Token* defValTok = i->nameToken()->next();
if (Token::Match(i->nameToken()->previous(), "* %var% ) (")) // function pointer. Jump behind parameter list.
defValTok = defValTok->linkAt(1)->next();
for (; defValTok; defValTok = defValTok->next()) { for (; defValTok; defValTok = defValTok->next()) {
if (defValTok->str() == "[") if (defValTok->str() == "[")
defValTok = defValTok->link(); defValTok = defValTok->link();

View File

@ -157,6 +157,7 @@ private:
TEST_CASE(localvarRangeBasedFor); // #7075 TEST_CASE(localvarRangeBasedFor); // #7075
TEST_CASE(localvarAssignInWhile); TEST_CASE(localvarAssignInWhile);
TEST_CASE(localvarTemplate); // #4955 - variable is used as template parameter TEST_CASE(localvarTemplate); // #4955 - variable is used as template parameter
TEST_CASE(localvarFuncPtr); // #7194
TEST_CASE(localvarCppInitialization); TEST_CASE(localvarCppInitialization);
TEST_CASE(localvarCpp11Initialization); TEST_CASE(localvarCpp11Initialization);
@ -3885,6 +3886,30 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void localvarFuncPtr() {
functionVariableUsage("int main() {\n"
" void(*funcPtr)(void)(x);\n"
"}");
TODO_ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'funcPtr' is assigned a value never used.\n", "", errout.str());
functionVariableUsage("int main() {\n"
" void(*funcPtr)(void);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Unused variable: funcPtr\n", errout.str());
functionVariableUsage("int main() {\n"
" void(*funcPtr)(void)(x);\n"
" funcPtr();\n"
"}");
ASSERT_EQUALS("", errout.str());
functionVariableUsage("int main() {\n"
" void(*funcPtr)(void) = x;\n"
" funcPtr();\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void chainedAssignment() { void chainedAssignment() {
// #5466 // #5466
functionVariableUsage("void NotUsed(double* pdD, int n) {\n" functionVariableUsage("void NotUsed(double* pdD, int n) {\n"