Cppcheck
checkvaarg.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 checkvaargtH
22 #define checkvaargtH
23 //---------------------------------------------------------------------------
24 
25 #include "check.h"
26 #include "config.h"
27 #include "tokenize.h"
28 
29 #include <string>
30 
31 class ErrorLogger;
32 class Settings;
33 class Token;
34 
35 /// @addtogroup Checks
36 /// @{
37 
38 /**
39  * @brief Checking for misusage of variable argument lists
40  */
41 
42 class CPPCHECKLIB CheckVaarg : public Check {
43 public:
44  CheckVaarg() : Check(myName()) {}
45 
46 private:
47  CheckVaarg(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
48  : Check(myName(), tokenizer, settings, errorLogger) {}
49 
50  void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override {
51  CheckVaarg check(&tokenizer, &tokenizer.getSettings(), errorLogger);
52  check.va_start_argument();
53  check.va_list_usage();
54  }
55 
56  void va_start_argument();
57  void va_list_usage();
58 
59  void wrongParameterTo_va_start_error(const Token *tok, const std::string& paramIsName, const std::string& paramShouldName);
60  void referenceAs_va_start_error(const Token *tok, const std::string& paramName);
61  void va_end_missingError(const Token *tok, const std::string& varname);
62  void va_list_usedBeforeStartedError(const Token *tok, const std::string& varname);
63  void va_start_subsequentCallsError(const Token *tok, const std::string& varname);
64 
65  void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override {
66  CheckVaarg c(nullptr, settings, errorLogger);
67  c.wrongParameterTo_va_start_error(nullptr, "arg1", "arg2");
68  c.referenceAs_va_start_error(nullptr, "arg1");
69  c.va_end_missingError(nullptr, "vl");
70  c.va_list_usedBeforeStartedError(nullptr, "vl");
71  c.va_start_subsequentCallsError(nullptr, "vl");
72  }
73 
74  static std::string myName() {
75  return "Vaarg";
76  }
77 
78  std::string classInfo() const override {
79  return "Check for misusage of variable argument lists:\n"
80  "- Wrong parameter passed to va_start()\n"
81  "- Reference passed to va_start()\n"
82  "- Missing va_end()\n"
83  "- Using va_list before it is opened\n"
84  "- Subsequent calls to va_start/va_copy()\n";
85  }
86 };
87 
88 /// @}
89 
90 //---------------------------------------------------------------------------
91 #endif // checkvaargtH
Checking for misusage of variable argument lists.
Definition: checkvaarg.h:42
void wrongParameterTo_va_start_error(const Token *tok, const std::string &paramIsName, const std::string &paramShouldName)
Definition: checkvaarg.cpp:84
static std::string myName()
Definition: checkvaarg.h:74
void va_list_usedBeforeStartedError(const Token *tok, const std::string &varname)
Definition: checkvaarg.cpp:172
void referenceAs_va_start_error(const Token *tok, const std::string &paramName)
Definition: checkvaarg.cpp:90
void va_end_missingError(const Token *tok, const std::string &varname)
Definition: checkvaarg.cpp:166
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
run checks, the token list is not simplified
Definition: checkvaarg.h:50
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override
get error messages
Definition: checkvaarg.h:65
CheckVaarg(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
Definition: checkvaarg.h:47
std::string classInfo() const override
get information about this class, used to generate documentation
Definition: checkvaarg.h:78
void va_start_subsequentCallsError(const Token *tok, const std::string &varname)
Definition: checkvaarg.cpp:178
void va_list_usage()
Definition: checkvaarg.cpp:101
void va_start_argument()
Definition: checkvaarg.cpp:50
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
#define CPPCHECKLIB
Definition: config.h:35