2011-08-19 20:35:25 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2016-01-01 14:34:45 +01:00
|
|
|
* Copyright (C) 2007-2016 Cppcheck team.
|
2011-08-19 20:35:25 +02:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#ifndef checkunusedvarH
|
|
|
|
#define checkunusedvarH
|
2011-08-19 20:35:25 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
#include "config.h"
|
2011-08-19 20:35:25 +02:00
|
|
|
#include "check.h"
|
2014-05-23 14:09:34 +02:00
|
|
|
|
|
|
|
#include <map>
|
2011-08-19 20:35:25 +02:00
|
|
|
|
2013-04-01 12:41:14 +02:00
|
|
|
class Type;
|
2011-12-18 20:15:41 +01:00
|
|
|
class Scope;
|
|
|
|
class Variables;
|
2011-08-19 20:35:25 +02:00
|
|
|
|
|
|
|
/// @addtogroup Checks
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief Various small checks */
|
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB CheckUnusedVar : public Check {
|
2011-08-19 20:35:25 +02:00
|
|
|
public:
|
|
|
|
/** @brief This constructor is used when registering the CheckClass */
|
2014-11-20 14:20:09 +01:00
|
|
|
CheckUnusedVar() : Check(myName()) {
|
2013-08-07 16:27:37 +02:00
|
|
|
}
|
2011-08-19 20:35:25 +02:00
|
|
|
|
|
|
|
/** @brief This constructor is used when running checks. */
|
|
|
|
CheckUnusedVar(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
2014-11-20 14:20:09 +01:00
|
|
|
: Check(myName(), tokenizer, settings, errorLogger) {
|
2013-08-07 16:27:37 +02:00
|
|
|
}
|
2011-08-19 20:35:25 +02:00
|
|
|
|
|
|
|
/** @brief Run checks against the normal token list */
|
2014-11-20 14:20:09 +01:00
|
|
|
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
|
2011-08-19 20:35:25 +02:00
|
|
|
CheckUnusedVar checkUnusedVar(tokenizer, settings, errorLogger);
|
|
|
|
|
|
|
|
// Coding style checks
|
|
|
|
checkUnusedVar.checkStructMemberUsage();
|
|
|
|
checkUnusedVar.checkFunctionVariableUsage();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @brief Run checks against the simplified token list */
|
2014-11-20 14:20:09 +01:00
|
|
|
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
|
2012-01-28 12:32:28 +01:00
|
|
|
(void)tokenizer;
|
|
|
|
(void)settings;
|
|
|
|
(void)errorLogger;
|
2011-08-19 20:35:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @brief %Check for unused function variables */
|
2012-12-04 21:39:51 +01:00
|
|
|
void checkFunctionVariableUsage_iterateScopes(const Scope* const scope, Variables& variables, bool insideLoop);
|
2011-08-19 20:35:25 +02:00
|
|
|
void checkFunctionVariableUsage();
|
|
|
|
|
|
|
|
/** @brief %Check that all struct members are used */
|
|
|
|
void checkStructMemberUsage();
|
|
|
|
|
2012-03-20 19:00:16 +01:00
|
|
|
private:
|
2013-04-01 12:41:14 +02:00
|
|
|
bool isRecordTypeWithoutSideEffects(const Type* type);
|
2014-02-01 22:40:35 +01:00
|
|
|
bool isEmptyType(const Type* type);
|
2013-04-01 12:41:14 +02:00
|
|
|
|
2011-08-19 20:35:25 +02:00
|
|
|
// Error messages..
|
2015-09-22 16:38:23 +02:00
|
|
|
void unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname, bool isUnion = false);
|
2011-08-19 20:35:25 +02:00
|
|
|
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);
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
|
2011-08-19 20:35:25 +02:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
static std::string myName() {
|
2011-08-19 20:35:25 +02:00
|
|
|
return "UnusedVar";
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
std::string classInfo() const {
|
2011-08-19 20:35:25 +02:00
|
|
|
return "UnusedVar checks\n"
|
|
|
|
|
|
|
|
// style
|
2014-09-30 14:56:12 +02:00
|
|
|
"- unused variable\n"
|
|
|
|
"- allocated but unused variable\n"
|
|
|
|
"- unred variable\n"
|
|
|
|
"- unassigned variable\n"
|
|
|
|
"- unused struct member\n";
|
2011-08-19 20:35:25 +02:00
|
|
|
}
|
2013-04-01 12:41:14 +02:00
|
|
|
|
|
|
|
std::map<const Type *,bool> isRecordTypeWithoutSideEffectsMap;
|
2014-02-01 22:40:35 +01:00
|
|
|
|
|
|
|
std::map<const Type *,bool> isEmptyTypeMap;
|
|
|
|
|
2011-08-19 20:35:25 +02:00
|
|
|
};
|
|
|
|
/// @}
|
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#endif // checkunusedvarH
|