Cppcheck
errorlogger.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 errorloggerH
21 #define errorloggerH
22 //---------------------------------------------------------------------------
23 
24 #include "config.h"
25 #include "errortypes.h"
26 #include "color.h"
27 
28 #include <cstddef>
29 #include <list>
30 #include <set>
31 #include <string>
32 #include <utility>
33 #include <vector>
34 
35 class Token;
36 class TokenList;
37 
38 namespace tinyxml2 {
39  class XMLElement;
40 }
41 
42 /// @addtogroup Core
43 /// @{
44 
45 /**
46  * Wrapper for error messages, provided by reportErr()
47  */
49 public:
50  /**
51  * File name and line number.
52  * Internally paths are stored with / separator. When getting the filename
53  * it is by default converted to native separators.
54  */
56  public:
57  FileLocation(const std::string &file, int line, unsigned int column)
58  : fileIndex(0), line(line), column(column), mOrigFileName(file), mFileName(file) {}
59 
60  FileLocation(const std::string &file, std::string info, int line, unsigned int column)
61  : fileIndex(0), line(line), column(column), mOrigFileName(file), mFileName(file), mInfo(std::move(info)) {}
62 
63  FileLocation(const Token* tok, const TokenList* tokenList);
64  FileLocation(const Token* tok, std::string info, const TokenList* tokenList);
65 
66  /**
67  * Return the filename.
68  * @param convert If true convert path to native separators.
69  * @return filename.
70  */
71  std::string getfile(bool convert = true) const;
72 
73  /**
74  * Filename with the whole path (no --rp)
75  * @param convert If true convert path to native separators.
76  * @return filename.
77  */
78  std::string getOrigFile(bool convert = true) const;
79 
80  /**
81  * Set the filename.
82  * @param file Filename to set.
83  */
84  void setfile(std::string file);
85 
86  /**
87  * @return the location as a string. Format: [file:line]
88  */
89  std::string stringify() const;
90 
91  unsigned int fileIndex;
92  int line; // negative value means "no line"
93  unsigned int column;
94 
95  const std::string& getinfo() const {
96  return mInfo;
97  }
98 
99  private:
100  std::string mOrigFileName;
101  std::string mFileName;
102  std::string mInfo;
103  };
104 
105  ErrorMessage(std::list<FileLocation> callStack,
106  std::string file1,
107  Severity severity,
108  const std::string &msg,
109  std::string id, Certainty certainty);
110  ErrorMessage(std::list<FileLocation> callStack,
111  std::string file1,
112  Severity severity,
113  const std::string &msg,
114  std::string id,
115  const CWE &cwe,
116  Certainty certainty);
117  ErrorMessage(const std::list<const Token*>& callstack,
118  const TokenList* list,
119  Severity severity,
120  std::string id,
121  const std::string& msg,
122  Certainty certainty);
123  ErrorMessage(const std::list<const Token*>& callstack,
124  const TokenList* list,
125  Severity severity,
126  std::string id,
127  const std::string& msg,
128  const CWE &cwe,
129  Certainty certainty);
130  ErrorMessage(const ErrorPath &errorPath,
131  const TokenList *tokenList,
132  Severity severity,
133  const char id[],
134  const std::string &msg,
135  const CWE &cwe,
136  Certainty certainty);
137  ErrorMessage();
138  explicit ErrorMessage(const tinyxml2::XMLElement * const errmsg);
139 
140  /**
141  * Format the error message in XML format
142  */
143  std::string toXML() const;
144 
145  static std::string getXMLHeader(std::string productName);
146  static std::string getXMLFooter();
147 
148  /**
149  * Format the error message into a string.
150  * @param verbose use verbose message
151  * @param templateFormat Empty string to use default output format
152  * or template to be used. E.g. "{file}:{line},{severity},{id},{message}"
153  * @param templateLocation Format Empty string to use default output format
154  * or template to be used. E.g. "{file}:{line},{info}"
155  * @return formatted string
156  */
157  std::string toString(bool verbose,
158  const std::string &templateFormat = emptyString,
159  const std::string &templateLocation = emptyString) const;
160 
161  std::string serialize() const;
162  void deserialize(const std::string &data);
163 
164  std::list<FileLocation> callStack;
165  std::string id;
166 
167  /** For GUI rechecking; source file (not header) */
168  std::string file0;
169 
173 
174  /** Warning hash */
175  std::size_t hash;
176 
177  /** set short and verbose messages */
178  void setmsg(const std::string &msg);
179 
180  /** Short message (single line short message) */
181  const std::string &shortMessage() const {
182  return mShortMessage;
183  }
184 
185  /** Verbose message (may be the same as the short message) */
186  const std::string &verboseMessage() const {
187  return mVerboseMessage;
188  }
189 
190  /** Symbol names */
191  const std::string &symbolNames() const {
192  return mSymbolNames;
193  }
194 
195  static ErrorMessage fromInternalError(const InternalError &internalError, const TokenList *tokenList, const std::string &filename, const std::string& msg = emptyString);
196 
197 private:
198  static std::string fixInvalidChars(const std::string& raw);
199 
200  /** Short message */
201  std::string mShortMessage;
202 
203  /** Verbose message */
204  std::string mVerboseMessage;
205 
206  /** symbol names */
207  std::string mSymbolNames;
208 };
209 
210 /**
211  * @brief This is an interface, which the class responsible of error logging
212  * should implement.
213  */
215 public:
216  ErrorLogger() = default;
217  virtual ~ErrorLogger() = default;
218 
219  /**
220  * Information about progress is directed here.
221  * Override this to receive the progress messages.
222  *
223  * @param outmsg Message to show e.g. "Checking main.cpp..."
224  */
225  virtual void reportOut(const std::string &outmsg, Color c = Color::Reset) = 0;
226 
227  /**
228  * Information about found errors and warnings is directed
229  * here. Override this to receive the errormessages.
230  *
231  * @param msg Location and other information about the found error.
232  */
233  virtual void reportErr(const ErrorMessage &msg) = 0;
234 
235  /**
236  * Report progress to client
237  * @param filename main file that is checked
238  * @param stage for example preprocess / tokenize / simplify / check
239  * @param value progress value (0-100)
240  */
241  virtual void reportProgress(const std::string &filename, const char stage[], const std::size_t value) {
242  (void)filename;
243  (void)stage;
244  (void)value;
245  }
246 
247  static std::string callStackToString(const std::list<ErrorMessage::FileLocation> &callStack);
248 
249  /**
250  * Convert XML-sensitive characters into XML entities
251  * @param str The input string containing XML-sensitive characters
252  * @return The output string containing XML entities
253  */
254  static std::string toxml(const std::string &str);
255 
256  static std::string plistHeader(const std::string &version, const std::vector<std::string> &files);
257  static std::string plistData(const ErrorMessage &msg);
258  static const char *plistFooter() {
259  return " </array>\r\n"
260  "</dict>\r\n"
261  "</plist>";
262  }
263 
264  static bool isCriticalErrorId(const std::string& id) {
265  return mCriticalErrorIds.count(id) != 0;
266  }
267 
268 private:
269  static const std::set<std::string> mCriticalErrorIds;
270 };
271 
272 /** Replace substring. Example replaceStr("1,NR,3", "NR", "2") => "1,2,3" */
273 std::string replaceStr(std::string s, const std::string &from, const std::string &to);
274 
275 /** replaces the static parts of the location template **/
276 CPPCHECKLIB void substituteTemplateFormatStatic(std::string& templateFormat);
277 
278 /** replaces the static parts of the location template **/
279 CPPCHECKLIB void substituteTemplateLocationStatic(std::string& templateLocation);
280 
281 /// @}
282 //---------------------------------------------------------------------------
283 #endif // errorloggerH
This is an interface, which the class responsible of error logging should implement.
Definition: errorlogger.h:214
ErrorLogger()=default
static bool isCriticalErrorId(const std::string &id)
Definition: errorlogger.h:264
static const char * plistFooter()
Definition: errorlogger.h:258
virtual void reportErr(const ErrorMessage &msg)=0
Information about found errors and warnings is directed here.
virtual ~ErrorLogger()=default
static const std::set< std::string > mCriticalErrorIds
Definition: errorlogger.h:269
virtual void reportOut(const std::string &outmsg, Color c=Color::Reset)=0
Information about progress is directed here.
virtual void reportProgress(const std::string &filename, const char stage[], const std::size_t value)
Report progress to client.
Definition: errorlogger.h:241
File name and line number.
Definition: errorlogger.h:55
FileLocation(const std::string &file, std::string info, int line, unsigned int column)
Definition: errorlogger.h:60
FileLocation(const std::string &file, int line, unsigned int column)
Definition: errorlogger.h:57
const std::string & getinfo() const
Definition: errorlogger.h:95
Wrapper for error messages, provided by reportErr()
Definition: errorlogger.h:48
std::string mSymbolNames
symbol names
Definition: errorlogger.h:207
Severity severity
Definition: errorlogger.h:170
std::string mVerboseMessage
Verbose message.
Definition: errorlogger.h:204
std::size_t hash
Warning hash.
Definition: errorlogger.h:175
const std::string & shortMessage() const
Short message (single line short message)
Definition: errorlogger.h:181
std::string mShortMessage
Short message.
Definition: errorlogger.h:201
const std::string & verboseMessage() const
Verbose message (may be the same as the short message)
Definition: errorlogger.h:186
std::string file0
For GUI rechecking; source file (not header)
Definition: errorlogger.h:168
std::list< FileLocation > callStack
Definition: errorlogger.h:164
std::string id
Definition: errorlogger.h:165
Certainty certainty
Definition: errorlogger.h:172
const std::string & symbolNames() const
Symbol names.
Definition: errorlogger.h:191
The token list that the TokenList generates is a linked-list of this class.
Definition: token.h:150
std::string toString(Color c)
Definition: color.cpp:54
Color
Definition: color.h:27
static const std::string emptyString
Definition: config.h:127
#define WARN_UNUSED
Definition: config.h:110
#define CPPCHECKLIB
Definition: config.h:35
Severity
enum class for severity.
Definition: errortypes.h:63
Certainty
Definition: errortypes.h:54
CPPCHECKLIB void substituteTemplateFormatStatic(std::string &templateFormat)
replaces the static parts of the location template
std::string replaceStr(std::string s, const std::string &from, const std::string &to)
Replace substring.
CPPCHECKLIB void substituteTemplateLocationStatic(std::string &templateLocation)
replaces the static parts of the location template
std::list< ErrorPathItem > ErrorPath
Definition: errortypes.h:130
Definition: check.h:31
Simple container to be thrown when internal error is detected.
Definition: errortypes.h:36