move unused variable checks from checkother to checkunusedvar

This commit is contained in:
Robert Reif 2011-08-19 14:35:25 -04:00
parent 9fbef3ca7b
commit 2dd93dff75
7 changed files with 1640 additions and 1513 deletions

View File

@ -63,6 +63,7 @@ LIBOBJ = lib/check64bit.o \
lib/checkstl.o \ lib/checkstl.o \
lib/checkuninitvar.o \ lib/checkuninitvar.o \
lib/checkunusedfunctions.o \ lib/checkunusedfunctions.o \
lib/checkunusedvar.o \
lib/cppcheck.o \ lib/cppcheck.o \
lib/errorlogger.o \ lib/errorlogger.o \
lib/executionpath.o \ lib/executionpath.o \
@ -216,6 +217,9 @@ lib/checkuninitvar.o: lib/checkuninitvar.cpp lib/checkuninitvar.h lib/check.h li
lib/checkunusedfunctions.o: lib/checkunusedfunctions.cpp lib/checkunusedfunctions.h lib/check.h lib/token.h lib/tokenize.h lib/settings.h lib/errorlogger.h lib/checkunusedfunctions.o: lib/checkunusedfunctions.cpp lib/checkunusedfunctions.h lib/check.h lib/token.h lib/tokenize.h lib/settings.h lib/errorlogger.h
$(CXX) $(CPPFLAGS) $(CXXFLAGS) ${INCLUDE_FOR_LIB} -c -o lib/checkunusedfunctions.o lib/checkunusedfunctions.cpp $(CXX) $(CPPFLAGS) $(CXXFLAGS) ${INCLUDE_FOR_LIB} -c -o lib/checkunusedfunctions.o lib/checkunusedfunctions.cpp
lib/checkunusedvar.o: lib/checkunusedvar.cpp lib/checkunusedvar.h lib/check.h lib/token.h lib/tokenize.h lib/settings.h lib/errorlogger.h lib/mathlib.h lib/symboldatabase.h
$(CXX) $(CPPFLAGS) $(CXXFLAGS) ${INCLUDE_FOR_LIB} -c -o lib/checkunusedvar.o lib/checkunusedvar.cpp
lib/cppcheck.o: lib/cppcheck.cpp lib/cppcheck.h lib/settings.h lib/errorlogger.h lib/checkunusedfunctions.h lib/check.h lib/token.h lib/tokenize.h lib/preprocessor.h lib/path.h lib/timer.h lib/cppcheck.o: lib/cppcheck.cpp lib/cppcheck.h lib/settings.h lib/errorlogger.h lib/checkunusedfunctions.h lib/check.h lib/token.h lib/tokenize.h lib/preprocessor.h lib/path.h lib/timer.h
$(CXX) $(CPPFLAGS) $(CXXFLAGS) ${INCLUDE_FOR_LIB} -c -o lib/cppcheck.o lib/cppcheck.cpp $(CXX) $(CPPFLAGS) $(CXXFLAGS) ${INCLUDE_FOR_LIB} -c -o lib/cppcheck.o lib/cppcheck.cpp

File diff suppressed because it is too large Load Diff

View File

@ -54,8 +54,6 @@ public:
checkOther.warningOldStylePointerCast(); checkOther.warningOldStylePointerCast();
checkOther.checkUnsignedDivision(); checkOther.checkUnsignedDivision();
checkOther.checkCharVariable(); checkOther.checkCharVariable();
checkOther.functionVariableUsage();
checkOther.checkStructMemberUsage();
checkOther.strPlusChar(); checkOther.strPlusChar();
checkOther.sizeofsizeof(); checkOther.sizeofsizeof();
checkOther.sizeofCalculation(); checkOther.sizeofCalculation();
@ -129,22 +127,12 @@ public:
/** @brief %Check for unsigned division */ /** @brief %Check for unsigned division */
void checkUnsignedDivision(); void checkUnsignedDivision();
/** @brief %Check for unused function variables */
void functionVariableUsage();
void unusedVariableError(const Token *tok, const std::string &varname);
void allocatedButUnusedVariableError(const Token *tok, const std::string &varname);
void unreadVariableError(const Token *tok, const std::string &varname);
void unassignedVariableError(const Token *tok, const std::string &varname);
/** @brief %Check scope of variables */ /** @brief %Check scope of variables */
void checkVariableScope(); void checkVariableScope();
/** @brief %Check for constant function parameter */ /** @brief %Check for constant function parameter */
void checkConstantFunctionParameter(); void checkConstantFunctionParameter();
/** @brief %Check that all struct members are used */
void checkStructMemberUsage();
/** @brief Using char variable as array index / as operand in bit operation */ /** @brief Using char variable as array index / as operand in bit operation */
void checkCharVariable(); void checkCharVariable();
@ -231,9 +219,6 @@ public:
/** @brief %Check for duplicate break statements in a switch or loop */ /** @brief %Check for duplicate break statements in a switch or loop */
void checkDuplicateBreak(); void checkDuplicateBreak();
/** @brief check if token is a record type without side effects */
bool isRecordTypeWithoutSideEffects(const Token *tok);
/** @brief assigning bool to pointer */ /** @brief assigning bool to pointer */
void checkAssignBoolToPointer(); void checkAssignBoolToPointer();
@ -245,7 +230,6 @@ public:
void dangerousUsageStrtolError(const Token *tok); void dangerousUsageStrtolError(const Token *tok);
void sprintfOverlappingDataError(const Token *tok, const std::string &varname); void sprintfOverlappingDataError(const Token *tok, const std::string &varname);
void udivError(const Token *tok); void udivError(const Token *tok);
void unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname);
void passedByValueError(const Token *tok, const std::string &parname); void passedByValueError(const Token *tok, const std::string &parname);
void constStatementError(const Token *tok, const std::string &type); void constStatementError(const Token *tok, const std::string &type);
void charArrayIndexError(const Token *tok); void charArrayIndexError(const Token *tok);
@ -296,7 +280,6 @@ public:
// style/warning // style/warning
c.cstyleCastError(0); c.cstyleCastError(0);
c.dangerousUsageStrtolError(0); c.dangerousUsageStrtolError(0);
c.unusedStructMemberError(0, "structname", "variable");
c.passedByValueError(0, "parametername"); c.passedByValueError(0, "parametername");
c.constStatementError(0, "type"); c.constStatementError(0, "type");
c.charArrayIndexError(0); c.charArrayIndexError(0);
@ -312,10 +295,6 @@ public:
c.invalidScanfError(0); c.invalidScanfError(0);
c.incorrectLogicOperatorError(0, true); c.incorrectLogicOperatorError(0, true);
c.secondAlwaysTrueFalseWhenFirstTrueError(0, "when first comparison is true, the 2nd comparison is always true"); c.secondAlwaysTrueFalseWhenFirstTrueError(0, "when first comparison is true, the 2nd comparison is always true");
c.unusedVariableError(0, "varname");
c.allocatedButUnusedVariableError(0, "varname");
c.unreadVariableError(0, "varname");
c.unassignedVariableError(0, "varname");
c.catchExceptionByValueError(0); c.catchExceptionByValueError(0);
c.memsetZeroBytesError(0, "varname"); c.memsetZeroBytesError(0, "varname");
c.clarifyCalculationError(0, "+"); c.clarifyCalculationError(0, "+");
@ -358,7 +337,6 @@ public:
"* bad usage of the function 'strtol'\n" "* bad usage of the function 'strtol'\n"
"* [[CheckUnsignedDivision|unsigned division]]\n" "* [[CheckUnsignedDivision|unsigned division]]\n"
"* Dangerous usage of 'scanf'\n" "* Dangerous usage of 'scanf'\n"
"* unused struct member\n"
"* passing parameter by value\n" "* passing parameter by value\n"
"* [[IncompleteStatement|Incomplete statement]]\n" "* [[IncompleteStatement|Incomplete statement]]\n"
"* [[charvar|check how signed char variables are used]]\n" "* [[charvar|check how signed char variables are used]]\n"

1516
lib/checkunusedvar.cpp Normal file

File diff suppressed because it is too large Load Diff

113
lib/checkunusedvar.h Normal file
View File

@ -0,0 +1,113 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2011 Daniel Marjamäki and Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
#ifndef CheckUnusedVarH
#define CheckUnusedVarH
//---------------------------------------------------------------------------
#include "check.h"
#include "settings.h"
class Token;
/// @addtogroup Checks
/// @{
/** @brief Various small checks */
class CheckUnusedVar : public Check
{
public:
/** @brief This constructor is used when registering the CheckClass */
CheckUnusedVar() : Check(myName())
{ }
/** @brief This constructor is used when running checks. */
CheckUnusedVar(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
: Check(myName(), tokenizer, settings, errorLogger)
{ }
/** @brief Run checks against the normal token list */
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{
CheckUnusedVar checkUnusedVar(tokenizer, settings, errorLogger);
// Coding style checks
checkUnusedVar.checkStructMemberUsage();
checkUnusedVar.checkFunctionVariableUsage();
}
/** @brief Run checks against the simplified token list */
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{
CheckUnusedVar checkUnusedVar(tokenizer, settings, errorLogger);
}
/** @brief %Check for unused function variables */
void checkFunctionVariableUsage();
/** @brief %Check that all struct members are used */
void checkStructMemberUsage();
// Error messages..
void unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname);
void unusedVariableError(const Token *tok, const std::string &varname);
void allocatedButUnusedVariableError(const Token *tok, const std::string &varname);
void unreadVariableError(const Token *tok, const std::string &varname);
void unassignedVariableError(const Token *tok, const std::string &varname);
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
CheckUnusedVar c(0, settings, errorLogger);
// style/warning
c.unusedVariableError(0, "varname");
c.allocatedButUnusedVariableError(0, "varname");
c.unreadVariableError(0, "varname");
c.unassignedVariableError(0, "varname");
c.unusedStructMemberError(0, "structname", "variable");
}
std::string myName() const
{
return "UnusedVar";
}
std::string classInfo() const
{
return "UnusedVar checks\n"
// style
"* unused variable\n"
"* allocated but unused variable\n"
"* unred variable\n"
"* unassigned variable\n"
"* unused struct member\n";
}
private:
/** @brief check if token is a record type without side effects */
bool isRecordTypeWithoutSideEffects(const Token *tok);
};
/// @}
//---------------------------------------------------------------------------
#endif

View File

@ -18,6 +18,7 @@ HEADERS += $${BASEPATH}check.h \
$${BASEPATH}checkstl.h \ $${BASEPATH}checkstl.h \
$${BASEPATH}checkuninitvar.h \ $${BASEPATH}checkuninitvar.h \
$${BASEPATH}checkunusedfunctions.h \ $${BASEPATH}checkunusedfunctions.h \
$${BASEPATH}checkunusedvar.h \
$${BASEPATH}cppcheck.h \ $${BASEPATH}cppcheck.h \
$${BASEPATH}errorlogger.h \ $${BASEPATH}errorlogger.h \
$${BASEPATH}executionpath.h \ $${BASEPATH}executionpath.h \
@ -45,6 +46,7 @@ SOURCES += $${BASEPATH}check64bit.cpp \
$${BASEPATH}checkstl.cpp \ $${BASEPATH}checkstl.cpp \
$${BASEPATH}checkuninitvar.cpp \ $${BASEPATH}checkuninitvar.cpp \
$${BASEPATH}checkunusedfunctions.cpp \ $${BASEPATH}checkunusedfunctions.cpp \
$${BASEPATH}checkunusedvar.cpp \
$${BASEPATH}cppcheck.cpp \ $${BASEPATH}cppcheck.cpp \
$${BASEPATH}errorlogger.cpp \ $${BASEPATH}errorlogger.cpp \
$${BASEPATH}executionpath.cpp \ $${BASEPATH}executionpath.cpp \

View File

@ -22,7 +22,7 @@
#include "testsuite.h" #include "testsuite.h"
#include "tokenize.h" #include "tokenize.h"
#include "checkother.h" #include "checkunusedvar.h"
#include <sstream> #include <sstream>
extern std::ostringstream errout; extern std::ostringstream errout;
@ -139,8 +139,8 @@ private:
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
// Check for unused variables.. // Check for unused variables..
CheckOther checkOther(&tokenizer, &settings, this); CheckUnusedVar checkUnusedVar(&tokenizer, &settings, this);
checkOther.checkStructMemberUsage(); checkUnusedVar.checkStructMemberUsage();
} }
void structmember1() void structmember1()
@ -376,8 +376,8 @@ private:
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
// Check for unused variables.. // Check for unused variables..
CheckOther checkOther(&tokenizer, &settings, this); CheckUnusedVar checkUnusedVar(&tokenizer, &settings, this);
checkOther.functionVariableUsage(); checkUnusedVar.checkFunctionVariableUsage();
} }
void localvar1() void localvar1()