Cppcheck
checkautovariables.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 //---------------------------------------------------------------------------
21 #ifndef checkautovariablesH
22 #define checkautovariablesH
23 //---------------------------------------------------------------------------
24 
25 #include "check.h"
26 #include "config.h"
27 #include "errortypes.h"
28 #include "tokenize.h"
29 
30 #include <string>
31 #include <set>
32 
33 class Settings;
34 class Token;
35 class ErrorLogger;
36 class Variable;
37 
38 namespace ValueFlow {
39  class Value;
40 }
41 
42 /// @addtogroup Checks
43 /** @brief Various small checks for automatic variables */
44 /// @{
45 
46 
48 public:
49  /** This constructor is used when registering the CheckClass */
50  CheckAutoVariables() : Check(myName()) {}
51 
52 private:
53  /** This constructor is used when running checks. */
54  CheckAutoVariables(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
55  : Check(myName(), tokenizer, settings, errorLogger) {}
56 
57  /** @brief Run checks against the normal token list */
58  void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override {
59  CheckAutoVariables checkAutoVariables(&tokenizer, &tokenizer.getSettings(), errorLogger);
60  checkAutoVariables.assignFunctionArg();
61  checkAutoVariables.checkVarLifetime();
62  checkAutoVariables.autoVariables();
63  }
64 
65  /** assign function argument */
66  void assignFunctionArg();
67 
68  /** Check auto variables */
69  void autoVariables();
70 
71  /**
72  * Check variable assignment.. value must be changed later or there will be a error reported
73  * @return true if error is reported */
74  bool checkAutoVariableAssignment(const Token *expr, bool inconclusive, const Token *startToken = nullptr);
75 
76  void checkVarLifetime();
77 
78  void checkVarLifetimeScope(const Token * start, const Token * end);
79 
80  void errorAutoVariableAssignment(const Token *tok, bool inconclusive);
81  void errorReturnDanglingLifetime(const Token *tok, const ValueFlow::Value* val);
82  void errorInvalidLifetime(const Token *tok, const ValueFlow::Value* val);
83  void errorDanglngLifetime(const Token *tok, const ValueFlow::Value *val);
84  void errorDanglingTemporaryLifetime(const Token* tok, const ValueFlow::Value* val, const Token* tempTok);
85  void errorReturnReference(const Token* tok, ErrorPath errorPath, bool inconclusive);
86  void errorDanglingReference(const Token *tok, const Variable *var, ErrorPath errorPath);
87  void errorDanglingTempReference(const Token* tok, ErrorPath errorPath, bool inconclusive);
88  void errorReturnTempReference(const Token* tok, ErrorPath errorPath, bool inconclusive);
89  void errorInvalidDeallocation(const Token *tok, const ValueFlow::Value *val);
90  void errorUselessAssignmentArg(const Token *tok);
91  void errorUselessAssignmentPtrArg(const Token *tok);
92 
93  void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override {
94  CheckAutoVariables c(nullptr,settings,errorLogger);
95  c.errorAutoVariableAssignment(nullptr, false);
96  c.errorReturnReference(nullptr, ErrorPath{}, false);
97  c.errorDanglingReference(nullptr, nullptr, ErrorPath{});
98  c.errorReturnTempReference(nullptr, ErrorPath{}, false);
99  c.errorDanglingTempReference(nullptr, ErrorPath{}, false);
100  c.errorInvalidDeallocation(nullptr, nullptr);
101  c.errorUselessAssignmentArg(nullptr);
102  c.errorUselessAssignmentPtrArg(nullptr);
103  c.errorReturnDanglingLifetime(nullptr, nullptr);
104  c.errorInvalidLifetime(nullptr, nullptr);
105  c.errorDanglngLifetime(nullptr, nullptr);
106  c.errorDanglingTemporaryLifetime(nullptr, nullptr, nullptr);
107  }
108 
109  static std::string myName() {
110  return "Auto Variables";
111  }
112 
113  std::string classInfo() const override {
114  return "A pointer to a variable is only valid as long as the variable is in scope.\n"
115  "Check:\n"
116  "- returning a pointer to auto or temporary variable\n"
117  "- assigning address of an variable to an effective parameter of a function\n"
118  "- returning reference to local/temporary variable\n"
119  "- returning address of function parameter\n"
120  "- suspicious assignment of pointer argument\n"
121  "- useless assignment of function argument\n";
122  }
123 
124  /** returns true if tokvalue has already been diagnosed */
125  bool diag(const Token* tokvalue);
126 
127  std::set<const Token*> mDiagDanglingTemp;
128 };
129 /// @}
130 //---------------------------------------------------------------------------
131 #endif // checkautovariablesH
Various small checks for automatic variables.
void errorUselessAssignmentPtrArg(const Token *tok)
void errorReturnReference(const Token *tok, ErrorPath errorPath, bool inconclusive)
void errorReturnDanglingLifetime(const Token *tok, const ValueFlow::Value *val)
void errorInvalidLifetime(const Token *tok, const ValueFlow::Value *val)
void errorDanglingTemporaryLifetime(const Token *tok, const ValueFlow::Value *val, const Token *tempTok)
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override
get error messages
std::string classInfo() const override
get information about this class, used to generate documentation
void errorDanglingTempReference(const Token *tok, ErrorPath errorPath, bool inconclusive)
static std::string myName()
void errorUselessAssignmentArg(const Token *tok)
void errorDanglingReference(const Token *tok, const Variable *var, ErrorPath errorPath)
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
Run checks against the normal token list.
void errorAutoVariableAssignment(const Token *tok, bool inconclusive)
void assignFunctionArg()
assign function argument
CheckAutoVariables(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
This constructor is used when running checks.
CheckAutoVariables()
This constructor is used when registering the CheckClass.
void errorDanglngLifetime(const Token *tok, const ValueFlow::Value *val)
void autoVariables()
Check auto variables.
void errorInvalidDeallocation(const Token *tok, const ValueFlow::Value *val)
std::set< const Token * > mDiagDanglingTemp
void errorReturnTempReference(const Token *tok, ErrorPath errorPath, bool inconclusive)
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 member variable.
#define CPPCHECKLIB
Definition: config.h:35
std::list< ErrorPathItem > ErrorPath
Definition: errortypes.h:130