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/checkuninitvar.o \
lib/checkunusedfunctions.o \
lib/checkunusedvar.o \
lib/cppcheck.o \
lib/errorlogger.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
$(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
$(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.checkUnsignedDivision();
checkOther.checkCharVariable();
checkOther.functionVariableUsage();
checkOther.checkStructMemberUsage();
checkOther.strPlusChar();
checkOther.sizeofsizeof();
checkOther.sizeofCalculation();
@ -129,22 +127,12 @@ public:
/** @brief %Check for unsigned division */
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 */
void checkVariableScope();
/** @brief %Check for constant function parameter */
void checkConstantFunctionParameter();
/** @brief %Check that all struct members are used */
void checkStructMemberUsage();
/** @brief Using char variable as array index / as operand in bit operation */
void checkCharVariable();
@ -231,9 +219,6 @@ public:
/** @brief %Check for duplicate break statements in a switch or loop */
void checkDuplicateBreak();
/** @brief check if token is a record type without side effects */
bool isRecordTypeWithoutSideEffects(const Token *tok);
/** @brief assigning bool to pointer */
void checkAssignBoolToPointer();
@ -245,7 +230,6 @@ public:
void dangerousUsageStrtolError(const Token *tok);
void sprintfOverlappingDataError(const Token *tok, const std::string &varname);
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 constStatementError(const Token *tok, const std::string &type);
void charArrayIndexError(const Token *tok);
@ -296,7 +280,6 @@ public:
// style/warning
c.cstyleCastError(0);
c.dangerousUsageStrtolError(0);
c.unusedStructMemberError(0, "structname", "variable");
c.passedByValueError(0, "parametername");
c.constStatementError(0, "type");
c.charArrayIndexError(0);
@ -312,10 +295,6 @@ public:
c.invalidScanfError(0);
c.incorrectLogicOperatorError(0, 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.memsetZeroBytesError(0, "varname");
c.clarifyCalculationError(0, "+");
@ -358,7 +337,6 @@ public:
"* bad usage of the function 'strtol'\n"
"* [[CheckUnsignedDivision|unsigned division]]\n"
"* Dangerous usage of 'scanf'\n"
"* unused struct member\n"
"* passing parameter by value\n"
"* [[IncompleteStatement|Incomplete statement]]\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}checkuninitvar.h \
$${BASEPATH}checkunusedfunctions.h \
$${BASEPATH}checkunusedvar.h \
$${BASEPATH}cppcheck.h \
$${BASEPATH}errorlogger.h \
$${BASEPATH}executionpath.h \
@ -45,6 +46,7 @@ SOURCES += $${BASEPATH}check64bit.cpp \
$${BASEPATH}checkstl.cpp \
$${BASEPATH}checkuninitvar.cpp \
$${BASEPATH}checkunusedfunctions.cpp \
$${BASEPATH}checkunusedvar.cpp \
$${BASEPATH}cppcheck.cpp \
$${BASEPATH}errorlogger.cpp \
$${BASEPATH}executionpath.cpp \

View File

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