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