Cppcheck
checkunusedvar.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2024 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 //---------------------------------------------------------------------------
20 #ifndef checkunusedvarH
21 #define checkunusedvarH
22 //---------------------------------------------------------------------------
23 
24 #include "check.h"
25 #include "config.h"
26 #include "tokenize.h"
27 
28 #include <list>
29 #include <map>
30 #include <string>
31 
32 class ErrorLogger;
33 class Scope;
34 class Settings;
35 class Token;
36 class Type;
37 class Variables;
38 class Variable;
39 class Function;
40 
41 /// @addtogroup Checks
42 /// @{
43 
44 
45 /** @brief Various small checks */
46 
48  friend class TestUnusedVar;
49 
50 public:
51  /** @brief This constructor is used when registering the CheckClass */
52  CheckUnusedVar() : Check(myName()) {}
53 
54 private:
55  /** @brief This constructor is used when running checks. */
56  CheckUnusedVar(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
57  : Check(myName(), tokenizer, settings, errorLogger) {}
58 
59  /** @brief Run checks against the normal token list */
60  void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override {
61  CheckUnusedVar checkUnusedVar(&tokenizer, &tokenizer.getSettings(), errorLogger);
62 
63  // Coding style checks
64  checkUnusedVar.checkStructMemberUsage();
65  checkUnusedVar.checkFunctionVariableUsage();
66  }
67 
68  /** @brief %Check for unused function variables */
69  void checkFunctionVariableUsage_iterateScopes(const Scope* const scope, Variables& variables);
70  void checkFunctionVariableUsage();
71 
72  /** @brief %Check that all struct members are used */
73  void checkStructMemberUsage();
74 
75  bool isRecordTypeWithoutSideEffects(const Type* type);
76  bool isVariableWithoutSideEffects(const Variable& var, const Type* type = nullptr);
77  bool isEmptyType(const Type* type);
78  bool isFunctionWithoutSideEffects(const Function& func, const Token* functionUsageToken,
79  std::list<const Function*> checkedFuncs);
80 
81  // Error messages..
82  void unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname, const std::string& prefix = "struct");
83  void unusedVariableError(const Token *tok, const std::string &varname);
84  void allocatedButUnusedVariableError(const Token *tok, const std::string &varname);
85  void unreadVariableError(const Token *tok, const std::string &varname, bool modified);
86  void unassignedVariableError(const Token *tok, const std::string &varname);
87 
88  void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override {
89  CheckUnusedVar c(nullptr, settings, errorLogger);
90  c.unusedVariableError(nullptr, "varname");
91  c.allocatedButUnusedVariableError(nullptr, "varname");
92  c.unreadVariableError(nullptr, "varname", false);
93  c.unassignedVariableError(nullptr, "varname");
94  c.unusedStructMemberError(nullptr, "structname", "variable");
95  }
96 
97  static std::string myName() {
98  return "UnusedVar";
99  }
100 
101  std::string classInfo() const override {
102  return "UnusedVar checks\n"
103 
104  // style
105  "- unused variable\n"
106  "- allocated but unused variable\n"
107  "- unread variable\n"
108  "- unassigned variable\n"
109  "- unused struct member\n";
110  }
111 
112  std::map<const Type *,bool> mIsRecordTypeWithoutSideEffectsMap;
113 
114  std::map<const Type *,bool> mIsEmptyTypeMap;
115 
116 };
117 /// @}
118 //---------------------------------------------------------------------------
119 #endif // checkunusedvarH
Various small checks.
void unassignedVariableError(const Token *tok, const std::string &varname)
std::map< const Type *, bool > mIsEmptyTypeMap
std::string classInfo() const override
get information about this class, used to generate documentation
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
Run checks against the normal token list.
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override
get error messages
void allocatedButUnusedVariableError(const Token *tok, const std::string &varname)
CheckUnusedVar()
This constructor is used when registering the CheckClass.
void unusedVariableError(const Token *tok, const std::string &varname)
void unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname, const std::string &prefix="struct")
void checkStructMemberUsage()
Check that all struct members are used
static std::string myName()
void unreadVariableError(const Token *tok, const std::string &varname, bool modified)
void checkFunctionVariableUsage()
CheckUnusedVar(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
This constructor is used when running checks.
std::map< const Type *, bool > mIsRecordTypeWithoutSideEffectsMap
Interface class that cppcheck uses to communicate with the checks.
Definition: check.h:59
This is an interface, which the class responsible of error logging should implement.
Definition: errorlogger.h:214
This is just a container for general settings so that we don't need to pass individual values to func...
Definition: settings.h:95
The token list that the TokenList generates is a linked-list of this class.
Definition: token.h:150
The main purpose is to tokenize the source code.
Definition: tokenize.h:46
const Settings & getSettings() const
Definition: tokenize.h:615
Information about a class type.
Information about a member variable.
This class is used create a list of variables within a function.
#define CPPCHECKLIB
Definition: config.h:35