2011-08-19 20:35:25 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2018-06-10 22:07:21 +02:00
|
|
|
* Copyright (C) 2007-2018 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
|
|
|
|
|
|
|
#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;
|
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 */
|
2018-05-15 16:37:40 +02: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 Run checks against the simplified token list */
|
2018-05-15 16:37:40 +02:00
|
|
|
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) override {
|
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);
|
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);
|
|
|
|
|
2018-05-15 16:37:40 +02: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";
|
|
|
|
}
|
|
|
|
|
2018-05-15 16:37:40 +02: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"
|
|
|
|
"- 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
|
|
|
|
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
|