Cppcheck
check.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 checkH
21 #define checkH
22 //---------------------------------------------------------------------------
23 
24 #include "config.h"
25 #include "errortypes.h"
26 
27 #include <list>
28 #include <string>
29 #include <utility>
30 
31 namespace tinyxml2 {
32  class XMLElement;
33 }
34 
35 namespace CTU {
36  class FileInfo;
37 }
38 
39 namespace ValueFlow {
40  class Value;
41 }
42 
43 class Settings;
44 class Token;
45 class ErrorLogger;
46 class ErrorMessage;
47 class Tokenizer;
48 
49 /** Use WRONG_DATA in checkers to mark conditions that check that data is correct */
50 #define WRONG_DATA(COND, TOK) ((COND) && wrongData((TOK), #COND))
51 
52 /// @addtogroup Core
53 /// @{
54 
55 /**
56  * @brief Interface class that cppcheck uses to communicate with the checks.
57  * All checking classes must inherit from this class
58  */
60 public:
61  /** This constructor is used when registering the CheckClass */
62  explicit Check(const std::string &aname);
63 
64 protected:
65  /** This constructor is used when running checks. */
66  Check(std::string aname, const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
67  : mTokenizer(tokenizer), mSettings(settings), mErrorLogger(errorLogger), mName(std::move(aname)) {}
68 
69 public:
70  virtual ~Check() {
71  if (!mTokenizer)
72  instances().remove(this);
73  }
74 
75  Check(const Check &) = delete;
76  Check& operator=(const Check &) = delete;
77 
78  /** List of registered check classes. This is used by Cppcheck to run checks and generate documentation */
79  static std::list<Check *> &instances();
80 
81  /** run checks, the token list is not simplified */
82  virtual void runChecks(const Tokenizer &, ErrorLogger *) = 0;
83 
84  /** get error messages */
85  virtual void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const = 0;
86 
87  /** class name, used to generate documentation */
88  const std::string& name() const {
89  return mName;
90  }
91 
92  /** get information about this class, used to generate documentation */
93  virtual std::string classInfo() const = 0;
94 
95  /**
96  * Write given error to stdout in xml format.
97  * This is for for printout out the error list with --errorlist
98  * @param errmsg Error message to write
99  */
100  static void writeToErrorList(const ErrorMessage &errmsg);
101 
102  /** Base class used for whole-program analysis */
104  public:
105  FileInfo() = default;
106  virtual ~FileInfo() = default;
107  virtual std::string toString() const {
108  return std::string();
109  }
110  };
111 
112  virtual FileInfo * getFileInfo(const Tokenizer& /*tokenizer*/, const Settings& /*settings*/) const {
113  return nullptr;
114  }
115 
116  virtual FileInfo * loadFileInfoFromXml(const tinyxml2::XMLElement *xmlElement) const {
117  (void)xmlElement;
118  return nullptr;
119  }
120 
121  // Return true if an error is reported.
122  virtual bool analyseWholeProgram(const CTU::FileInfo *ctu, const std::list<FileInfo*> &fileInfo, const Settings& /*settings*/, ErrorLogger & /*errorLogger*/) {
123  (void)ctu;
124  (void)fileInfo;
125  //(void)settings;
126  //(void)errorLogger;
127  return false;
128  }
129 
130 protected:
131  static std::string getMessageId(const ValueFlow::Value &value, const char id[]);
132 
133  const Tokenizer* const mTokenizer{};
134  const Settings* const mSettings{};
135  ErrorLogger* const mErrorLogger{};
136 
137  /** report an error */
138  void reportError(const Token *tok, const Severity severity, const std::string &id, const std::string &msg) {
139  reportError(tok, severity, id, msg, CWE(0U), Certainty::normal);
140  }
141 
142  /** report an error */
143  void reportError(const Token *tok, const Severity severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty) {
144  const std::list<const Token *> callstack(1, tok);
145  reportError(callstack, severity, id, msg, cwe, certainty);
146  }
147 
148  /** report an error */
149  void reportError(const std::list<const Token *> &callstack, Severity severity, const std::string &id, const std::string &msg) {
150  reportError(callstack, severity, id, msg, CWE(0U), Certainty::normal);
151  }
152 
153  /** report an error */
154  void reportError(const std::list<const Token *> &callstack, Severity severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty);
155 
156  void reportError(const ErrorPath &errorPath, Severity severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty);
157 
158  /** log checker */
159  void logChecker(const char id[]);
160 
161  ErrorPath getErrorPath(const Token* errtok, const ValueFlow::Value* value, std::string bug) const;
162 
163  /**
164  * Use WRONG_DATA in checkers when you check for wrong data. That
165  * will call this method
166  */
167  bool wrongData(const Token *tok, const char *str);
168 
169 private:
170  const std::string mName;
171 };
172 
173 /// @}
174 //---------------------------------------------------------------------------
175 #endif // checkH
#define logChecker(id)
Base class used for whole-program analysis.
Definition: check.h:103
virtual ~FileInfo()=default
virtual std::string toString() const
Definition: check.h:107
FileInfo()=default
Interface class that cppcheck uses to communicate with the checks.
Definition: check.h:59
Check(std::string aname, const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
This constructor is used when running checks.
Definition: check.h:66
virtual bool analyseWholeProgram(const CTU::FileInfo *ctu, const std::list< FileInfo * > &fileInfo, const Settings &, ErrorLogger &)
Definition: check.h:122
void reportError(const Token *tok, const Severity severity, const std::string &id, const std::string &msg)
report an error
Definition: check.h:138
virtual void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const =0
get error messages
Check(const Check &)=delete
virtual ~Check()
Definition: check.h:70
virtual void runChecks(const Tokenizer &, ErrorLogger *)=0
run checks, the token list is not simplified
Check & operator=(const Check &)=delete
virtual FileInfo * loadFileInfoFromXml(const tinyxml2::XMLElement *xmlElement) const
Definition: check.h:116
void reportError(const std::list< const Token * > &callstack, Severity severity, const std::string &id, const std::string &msg)
report an error
Definition: check.h:149
virtual FileInfo * getFileInfo(const Tokenizer &, const Settings &) const
Definition: check.h:112
virtual std::string classInfo() const =0
get information about this class, used to generate documentation
const std::string & name() const
class name, used to generate documentation
Definition: check.h:88
void reportError(const Token *tok, const Severity severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty)
report an error
Definition: check.h:143
const std::string mName
Definition: check.h:170
This is an interface, which the class responsible of error logging should implement.
Definition: errorlogger.h:214
Wrapper for error messages, provided by reportErr()
Definition: errorlogger.h:48
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
#define CPPCHECKLIB
Definition: config.h:35
Severity
enum class for severity.
Definition: errortypes.h:63
Certainty
Definition: errortypes.h:54
std::list< ErrorPathItem > ErrorPath
Definition: errortypes.h:130
Whole program analysis (ctu=Cross Translation Unit)
Definition: check.h:35
Definition: check.h:31
static constexpr char CWE[]
Definition: resultstree.cpp:67