2011-08-19 20:35:25 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2021-09-26 11:34:56 +02:00
|
|
|
* Copyright (C) 2007-2021 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
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "check.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "config.h"
|
2014-05-23 14:09:34 +02:00
|
|
|
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <list>
|
2014-05-23 14:09:34 +02:00
|
|
|
#include <map>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <string>
|
2011-08-19 20:35:25 +02:00
|
|
|
|
2017-05-27 04:33:47 +02:00
|
|
|
class ErrorLogger;
|
2011-12-18 20:15:41 +01:00
|
|
|
class Scope;
|
2017-05-27 04:33:47 +02:00
|
|
|
class Settings;
|
|
|
|
class Token;
|
|
|
|
class Tokenizer;
|
|
|
|
class Type;
|
2011-12-18 20:15:41 +01:00
|
|
|
class Variables;
|
2020-10-25 07:11:45 +01:00
|
|
|
class Variable;
|
2020-11-26 17:34:42 +01:00
|
|
|
class Function;
|
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 */
|
2021-08-07 20:51:18 +02:00
|
|
|
CheckUnusedVar() : Check(myName()) {}
|
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)
|
2021-08-07 20:51:18 +02:00
|
|
|
: Check(myName(), tokenizer, settings, errorLogger) {}
|
2011-08-19 20:35:25 +02:00
|
|
|
|
|
|
|
/** @brief Run checks against the normal token list */
|
2019-01-12 07:37:42 +01:00
|
|
|
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) OVERRIDE {
|
2011-08-19 20:35:25 +02:00
|
|
|
CheckUnusedVar checkUnusedVar(tokenizer, settings, errorLogger);
|
|
|
|
|
|
|
|
// Coding style checks
|
|
|
|
checkUnusedVar.checkStructMemberUsage();
|
|
|
|
checkUnusedVar.checkFunctionVariableUsage();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @brief %Check for unused function variables */
|
2018-12-17 17:48:45 +01:00
|
|
|
void checkFunctionVariableUsage_iterateScopes(const Scope* const scope, Variables& variables);
|
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);
|
2020-10-25 07:11:45 +01:00
|
|
|
bool isVariableWithoutSideEffects(const Variable& var);
|
2014-02-01 22:40:35 +01:00
|
|
|
bool isEmptyType(const Type* type);
|
2020-11-26 17:34:42 +01:00
|
|
|
bool isFunctionWithoutSideEffects(const Function& func, const Token* functionUsageToken,
|
2020-11-28 05:52:35 +01:00
|
|
|
std::list<const Function*> checkedFuncs);
|
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);
|
2016-10-10 21:27:40 +02:00
|
|
|
void unreadVariableError(const Token *tok, const std::string &varname, bool modified);
|
2011-08-19 20:35:25 +02:00
|
|
|
void unassignedVariableError(const Token *tok, const std::string &varname);
|
|
|
|
|
2019-01-12 07:37:42 +01:00
|
|
|
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const OVERRIDE {
|
2016-05-07 16:30:54 +02:00
|
|
|
CheckUnusedVar c(nullptr, settings, errorLogger);
|
2011-08-19 20:35:25 +02:00
|
|
|
|
|
|
|
// style/warning
|
2016-05-07 16:30:54 +02:00
|
|
|
c.unusedVariableError(nullptr, "varname");
|
|
|
|
c.allocatedButUnusedVariableError(nullptr, "varname");
|
2016-10-10 21:27:40 +02:00
|
|
|
c.unreadVariableError(nullptr, "varname", false);
|
2016-05-07 16:30:54 +02:00
|
|
|
c.unassignedVariableError(nullptr, "varname");
|
|
|
|
c.unusedStructMemberError(nullptr, "structname", "variable");
|
2011-08-19 20:35:25 +02:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
static std::string myName() {
|
2011-08-19 20:35:25 +02:00
|
|
|
return "UnusedVar";
|
|
|
|
}
|
|
|
|
|
2019-01-12 07:37:42 +01:00
|
|
|
std::string classInfo() const OVERRIDE {
|
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"
|
2020-01-05 09:32:39 +01:00
|
|
|
"- unread variable\n"
|
2014-09-30 14:56:12 +02:00
|
|
|
"- unassigned variable\n"
|
|
|
|
"- unused struct member\n";
|
2011-08-19 20:35:25 +02:00
|
|
|
}
|
2013-04-01 12:41:14 +02:00
|
|
|
|
2018-06-17 19:04:06 +02:00
|
|
|
std::map<const Type *,bool> mIsRecordTypeWithoutSideEffectsMap;
|
2014-02-01 22:40:35 +01:00
|
|
|
|
2018-06-17 19:04:06 +02:00
|
|
|
std::map<const Type *,bool> mIsEmptyTypeMap;
|
2014-02-01 22:40:35 +01:00
|
|
|
|
2011-08-19 20:35:25 +02:00
|
|
|
};
|
|
|
|
/// @}
|
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#endif // checkunusedvarH
|