Refactorized CheckUnusedVar

This commit is contained in:
PKEuS 2011-12-18 20:15:41 +01:00 committed by Daniel Marjamäki
parent b6b97fa43d
commit 6dc2a6e7ab
4 changed files with 369 additions and 672 deletions

View File

@ -1398,7 +1398,7 @@ void CheckOther::checkWrongPrintfScanfArguments()
case 'G': case 'G':
if (varTypeTok && varTypeTok->str() == "const") if (varTypeTok && varTypeTok->str() == "const")
varTypeTok = varTypeTok->next(); varTypeTok = varTypeTok->next();
if (varTypeTok && (isKnownType(variableInfo, varTypeTok) && !Token::Match(varTypeTok, "float|double") || variableInfo->isPointer() || variableInfo->isArray())) if (varTypeTok && ((isKnownType(variableInfo, varTypeTok) && !Token::Match(varTypeTok, "float|double")) || variableInfo->isPointer() || variableInfo->isArray()))
invalidPrintfArgTypeError_float(tok, numFormat, *i); invalidPrintfArgTypeError_float(tok, numFormat, *i);
else if (Token::Match(argListTok, "%str%")) else if (Token::Match(argListTok, "%str%"))
invalidPrintfArgTypeError_float(tok, numFormat, *i); invalidPrintfArgTypeError_float(tok, numFormat, *i);

File diff suppressed because it is too large Load Diff

View File

@ -26,6 +26,8 @@
#include "settings.h" #include "settings.h"
class Token; class Token;
class Scope;
class Variables;
/// @addtogroup Checks /// @addtogroup Checks
/// @{ /// @{
@ -59,6 +61,8 @@ public:
} }
/** @brief %Check for unused function variables */ /** @brief %Check for unused function variables */
void checkFunctionVariableUsage_iterateScopes(const Scope* const scope, Variables& variables);
void checkVariableUsage(const Scope* const scope, const Token* start, Variables& variables);
void checkFunctionVariableUsage(); void checkFunctionVariableUsage();
/** @brief %Check that all struct members are used */ /** @brief %Check that all struct members are used */
@ -96,10 +100,6 @@ public:
"* unassigned variable\n" "* unassigned variable\n"
"* unused struct member\n"; "* unused struct member\n";
} }
private:
/** @brief check if token is a record type without side effects */
bool isRecordTypeWithoutSideEffects(const Token *tok);
}; };
/// @} /// @}
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -43,7 +43,7 @@ private:
TEST_CASE(structmember7); TEST_CASE(structmember7);
TEST_CASE(structmember8); TEST_CASE(structmember8);
TEST_CASE(structmember9); // #2017 - struct is inherited TEST_CASE(structmember9); // #2017 - struct is inherited
TEST_CASE(structmember_extern); // No false positives for extern structs TEST_CASE(structmember_extern); // No false positives for extern structs
TEST_CASE(structmember10); TEST_CASE(structmember10);
TEST_CASE(localvar1); TEST_CASE(localvar1);
@ -83,6 +83,7 @@ private:
TEST_CASE(localvar35); // ticket #2535 TEST_CASE(localvar35); // ticket #2535
TEST_CASE(localvar36); // ticket #2805 TEST_CASE(localvar36); // ticket #2805
TEST_CASE(localvar37); // ticket #3078 TEST_CASE(localvar37); // ticket #3078
TEST_CASE(localvar38);
TEST_CASE(localvaralias1); TEST_CASE(localvaralias1);
TEST_CASE(localvaralias2); // ticket #1637 TEST_CASE(localvaralias2); // ticket #1637
TEST_CASE(localvaralias3); // ticket #1639 TEST_CASE(localvaralias3); // ticket #1639
@ -1359,6 +1360,16 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void localvar38() {
functionVariableUsage("std::string f() {\n"
" const char code[] = \"foo\";\n"
" const std::string s1(sizeof_(code));\n"
" const std::string s2 = sizeof_(code);\n"
" return(s1+s2);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void localvaralias1() { void localvaralias1() {
functionVariableUsage("void foo()\n" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -2287,9 +2298,7 @@ private:
" } while(a--);\n" " } while(a--);\n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:4]: (style) Unused variable: x\n" TODO_ASSERT_EQUALS("[test.cpp:4]: (style) Unused variable: x\n"
"[test.cpp:4]: (style) Unused variable: z\n", "[test.cpp:4]: (style) Unused variable: z\n", "", errout.str());
"", errout.str());
} }
void localvarStruct4() { void localvarStruct4() {
@ -2477,6 +2486,12 @@ private:
" for (;a;);\n" " for (;a;);\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
functionVariableUsage("void foo() {\n"
" for (int i = 0; (pci = cdi_list_get(pciDevices, i)); i++)\n"
" {}\n"
"}");
ASSERT_EQUALS("", errout.str());
} }
void localvarShift1() { void localvarShift1() {