Cppcheck
suppressions.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 #ifndef suppressionsH
20 #define suppressionsH
21 //---------------------------------------------------------------------------
22 
23 #include "config.h"
24 
25 #include <cstddef>
26 #include <istream>
27 #include <list>
28 #include <set>
29 #include <string>
30 #include <utility>
31 #include <vector>
32 
33 /// @addtogroup Core
34 /// @{
35 
36 class Tokenizer;
37 class ErrorMessage;
38 class ErrorLogger;
39 enum class Certainty;
40 
41 /** @brief class for handling suppressions */
43 public:
44 
45  enum class Type {
46  unique, file, block, blockBegin, blockEnd, macro
47  };
48 
50  std::size_t hash;
51  std::string errorId;
52  void setFileName(std::string s);
53  const std::string &getFileName() const {
54  return mFileName;
55  }
58  std::string symbolNames;
59  std::set<std::string> macroNames;
60 
61  static SuppressionList::ErrorMessage fromErrorMessage(const ::ErrorMessage &msg, const std::set<std::string> &macroNames);
62  private:
63  std::string mFileName;
64  };
65 
67  Suppression() = default;
68  Suppression(std::string id, std::string file, int line=NO_LINE) : errorId(std::move(id)), fileName(std::move(file)), lineNumber(line) {}
69 
70  bool operator<(const Suppression &other) const {
71  if (errorId != other.errorId)
72  return errorId < other.errorId;
73  if (lineNumber < other.lineNumber)
74  return true;
75  if (fileName != other.fileName)
76  return fileName < other.fileName;
77  if (symbolName != other.symbolName)
78  return symbolName < other.symbolName;
79  if (macroName != other.macroName)
80  return macroName < other.macroName;
81  if (hash != other.hash)
82  return hash < other.hash;
83  if (thisAndNextLine != other.thisAndNextLine)
84  return thisAndNextLine;
85  return false;
86  }
87 
88  bool operator==(const Suppression &other) const {
89  if (errorId != other.errorId)
90  return false;
91  if (lineNumber < other.lineNumber)
92  return false;
93  if (fileName != other.fileName)
94  return false;
95  if (symbolName != other.symbolName)
96  return false;
97  if (macroName != other.macroName)
98  return false;
99  if (hash != other.hash)
100  return false;
101  if (type != other.type)
102  return false;
103  if (lineBegin != other.lineBegin)
104  return false;
105  if (lineEnd != other.lineEnd)
106  return false;
107  return true;
108  }
109 
110  /**
111  * Parse inline suppression in comment
112  * @param comment the full comment text
113  * @param errorMessage output parameter for error message (wrong suppression attribute)
114  * @return true if it is a inline comment.
115  */
116  bool parseComment(std::string comment, std::string *errorMessage);
117 
118  bool isSuppressed(const ErrorMessage &errmsg) const;
119 
120  bool isMatch(const ErrorMessage &errmsg);
121 
122  std::string getText() const;
123 
124  bool isLocal() const {
125  return !fileName.empty() && fileName.find_first_of("?*") == std::string::npos;
126  }
127 
128  bool isSameParameters(const Suppression &other) const {
129  return errorId == other.errorId &&
130  fileName == other.fileName &&
131  lineNumber == other.lineNumber &&
132  symbolName == other.symbolName &&
133  hash == other.hash &&
134  thisAndNextLine == other.thisAndNextLine;
135  }
136 
137  std::string errorId;
138  std::string fileName;
139  int lineNumber = NO_LINE;
140  int lineBegin = NO_LINE;
141  int lineEnd = NO_LINE;
142  Type type = Type::unique;
143  std::string symbolName;
144  std::string macroName;
145  std::size_t hash{};
146  bool thisAndNextLine{}; // Special case for backwards compatibility: { // cppcheck-suppress something
147  bool matched{};
148  bool checked{}; // for inline suppressions, checked or not
149 
150  enum { NO_LINE = -1 };
151  };
152 
153  /**
154  * @brief Don't show errors listed in the file.
155  * @param istr Open file stream where errors can be read.
156  * @return error message. empty upon success
157  */
158  std::string parseFile(std::istream &istr);
159 
160  /**
161  * @brief Don't show errors listed in the file.
162  * @param filename file name
163  * @return error message. empty upon success
164  */
165  std::string parseXmlFile(const char *filename);
166 
167  /**
168  * Parse multi inline suppression in comment
169  * @param comment the full comment text
170  * @param errorMessage output parameter for error message (wrong suppression attribute)
171  * @return empty vector if something wrong.
172  */
173  static std::vector<Suppression> parseMultiSuppressComment(const std::string &comment, std::string *errorMessage);
174 
175  /**
176  * @brief Don't show the given error.
177  * @param line Description of error to suppress (in id:file:line format).
178  * @return error message. empty upon success
179  */
180  std::string addSuppressionLine(const std::string &line);
181 
182  /**
183  * @brief Don't show this error. File and/or line are optional. In which case
184  * the errorId alone is used for filtering.
185  * @param suppression suppression details
186  * @return error message. empty upon success
187  */
188  std::string addSuppression(Suppression suppression);
189 
190  /**
191  * @brief Combine list of suppressions into the current suppressions.
192  * @param suppressions list of suppression details
193  * @return error message. empty upon success
194  */
195  std::string addSuppressions(std::list<Suppression> suppressions);
196 
197  /**
198  * @brief Returns true if this message should not be shown to the user.
199  * @param errmsg error message
200  * @param global use global suppressions
201  * @return true if this error is suppressed.
202  */
203  bool isSuppressed(const ErrorMessage &errmsg, bool global = true);
204 
205  /**
206  * @brief Returns true if this message is "explicitly" suppressed. The suppression "id" must match textually exactly.
207  * @param errmsg error message
208  * @param global use global suppressions
209  * @return true if this error is explicitly suppressed.
210  */
211  bool isSuppressedExplicitly(const ErrorMessage &errmsg, bool global = true);
212 
213  /**
214  * @brief Returns true if this message should not be shown to the user.
215  * @param errmsg error message
216  * @return true if this error is suppressed.
217  */
218  bool isSuppressed(const ::ErrorMessage &errmsg, const std::set<std::string>& macroNames);
219 
220  /**
221  * @brief Create an xml dump of suppressions
222  * @param out stream to write XML to
223  */
224  void dump(std::ostream &out) const;
225 
226  /**
227  * @brief Returns list of unmatched local (per-file) suppressions.
228  * @return list of unmatched suppressions
229  */
230  std::list<Suppression> getUnmatchedLocalSuppressions(const std::string &file, const bool unusedFunctionChecking) const;
231 
232  /**
233  * @brief Returns list of unmatched global (glob pattern) suppressions.
234  * @return list of unmatched suppressions
235  */
236  std::list<Suppression> getUnmatchedGlobalSuppressions(const bool unusedFunctionChecking) const;
237 
238  /**
239  * @brief Returns list of all suppressions.
240  * @return list of suppressions
241  */
242  const std::list<Suppression> &getSuppressions() const;
243 
244  /**
245  * @brief Marks Inline Suppressions as checked if source line is in the token stream
246  */
247  void markUnmatchedInlineSuppressionsAsChecked(const Tokenizer &tokenizer);
248 
249  /**
250  * Report unmatched suppressions
251  * @param unmatched list of unmatched suppressions (from Settings::Suppressions::getUnmatched(Local|Global)Suppressions)
252  * @return true is returned if errors are reported
253  */
254  static bool reportUnmatchedSuppressions(const std::list<SuppressionList::Suppression> &unmatched, ErrorLogger &errorLogger);
255 
256 private:
257  /** @brief List of error which the user doesn't want to see. */
258  std::list<Suppression> mSuppressions;
259 };
260 
262 {
263  /** @brief suppress message (--suppressions) */
265  /** @brief suppress exitcode */
267 };
268 
269 /// @}
270 //---------------------------------------------------------------------------
271 #endif // suppressionsH
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
class for handling suppressions
Definition: suppressions.h:42
std::list< Suppression > mSuppressions
List of error which the user doesn't want to see.
Definition: suppressions.h:258
The main purpose is to tokenize the source code.
Definition: tokenize.h:46
Information about a class type.
#define CPPCHECKLIB
Definition: config.h:35
Certainty
Definition: errortypes.h:54
const std::string & getFileName() const
Definition: suppressions.h:53
std::set< std::string > macroNames
Definition: suppressions.h:59
bool isSameParameters(const Suppression &other) const
Definition: suppressions.h:128
bool operator<(const Suppression &other) const
Definition: suppressions.h:70
Suppression(std::string id, std::string file, int line=NO_LINE)
Definition: suppressions.h:68
bool operator==(const Suppression &other) const
Definition: suppressions.h:88
SuppressionList nofail
suppress exitcode
Definition: suppressions.h:266
SuppressionList nomsg
suppress message (–suppressions)
Definition: suppressions.h:264