forked from danmar/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorlogger.cpp
More file actions
492 lines (424 loc) · 16.7 KB
/
errorlogger.cpp
File metadata and controls
492 lines (424 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2016 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "errorlogger.h"
#include "path.h"
#include "cppcheck.h"
#include "tokenlist.h"
#include "token.h"
#include "utils.h"
#include <tinyxml2.h>
#include <cassert>
#include <iomanip>
#include <sstream>
#include <array>
InternalError::InternalError(const Token *tok, const std::string &errorMsg, Type type) :
token(tok), errorMessage(errorMsg)
{
switch (type) {
case SYNTAX:
id = "syntaxError";
break;
case INTERNAL:
id = "cppcheckError";
break;
}
}
ErrorLogger::ErrorMessage::ErrorMessage()
: _severity(Severity::none), _cwe(0U), _inconclusive(false)
{
}
ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &callStack, const std::string& file0_, Severity::SeverityType severity, const std::string &msg, const std::string &id, bool inconclusive) :
_callStack(callStack), // locations for this error message
_id(id), // set the message id
file0(file0_),
_severity(severity), // severity for this error message
_cwe(0U),
_inconclusive(inconclusive)
{
// set the summary and verbose messages
setmsg(msg);
}
ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &callStack, const std::string& file0_, Severity::SeverityType severity, const std::string &msg, const std::string &id, const CWE &cwe, bool inconclusive) :
_callStack(callStack), // locations for this error message
_id(id), // set the message id
file0(file0_),
_severity(severity), // severity for this error message
_cwe(cwe.id),
_inconclusive(inconclusive)
{
// set the summary and verbose messages
setmsg(msg);
}
ErrorLogger::ErrorMessage::ErrorMessage(const std::list<const Token*>& callstack, const TokenList* list, Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive)
: _id(id), _severity(severity), _cwe(0U), _inconclusive(inconclusive)
{
// Format callstack
for (std::list<const Token *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it) {
// --errorlist can provide null values here
if (!(*it))
continue;
_callStack.push_back(ErrorLogger::ErrorMessage::FileLocation(*it, list));
}
if (list && !list->getFiles().empty())
file0 = list->getFiles()[0];
setmsg(msg);
}
ErrorLogger::ErrorMessage::ErrorMessage(const std::list<const Token*>& callstack, const TokenList* list, Severity::SeverityType severity, const std::string& id, const std::string& msg, const CWE &cwe, bool inconclusive)
: _id(id), _severity(severity), _cwe(cwe.id), _inconclusive(inconclusive)
{
// Format callstack
for (std::list<const Token *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it) {
// --errorlist can provide null values here
if (!(*it))
continue;
_callStack.push_back(ErrorLogger::ErrorMessage::FileLocation(*it, list));
}
if (list && !list->getFiles().empty())
file0 = list->getFiles()[0];
setmsg(msg);
}
void ErrorLogger::ErrorMessage::setmsg(const std::string &msg)
{
// If a message ends to a '\n' and contains only a one '\n'
// it will cause the _verboseMessage to be empty which will show
// as an empty message to the user if --verbose is used.
// Even this doesn't cause problems with messages that have multiple
// lines, none of the the error messages should end into it.
assert(!(msg.back() =='\n'));
// The summary and verbose message are separated by a newline
// If there is no newline then both the summary and verbose messages
// are the given message
const std::string::size_type pos = msg.find('\n');
if (pos == std::string::npos) {
_shortMessage = msg;
_verboseMessage = msg;
} else {
_shortMessage = msg.substr(0, pos);
_verboseMessage = msg.substr(pos + 1);
}
}
std::string ErrorLogger::ErrorMessage::serialize() const
{
// Serialize this message into a simple string
std::ostringstream oss;
oss << _id.length() << " " << _id;
oss << Severity::toString(_severity).length() << " " << Severity::toString(_severity);
oss << MathLib::toString(_cwe.id).length() << " " << MathLib::toString(_cwe.id);
if (_inconclusive) {
const std::string inconclusive("inconclusive");
oss << inconclusive.length() << " " << inconclusive;
}
const std::string saneShortMessage = fixInvalidChars(_shortMessage);
const std::string saneVerboseMessage = fixInvalidChars(_verboseMessage);
oss << saneShortMessage.length() << " " << saneShortMessage;
oss << saneVerboseMessage.length() << " " << saneVerboseMessage;
oss << _callStack.size() << " ";
for (std::list<ErrorLogger::ErrorMessage::FileLocation>::const_iterator tok = _callStack.begin(); tok != _callStack.end(); ++tok) {
std::ostringstream smallStream;
smallStream << (*tok).line << ":" << (*tok).getfile();
oss << smallStream.str().length() << " " << smallStream.str();
}
return oss.str();
}
bool ErrorLogger::ErrorMessage::deserialize(const std::string &data)
{
_inconclusive = false;
_callStack.clear();
std::istringstream iss(data);
std::array<std::string, 5> results;
std::size_t elem = 0;
while (iss.good()) {
unsigned int len = 0;
if (!(iss >> len))
return false;
iss.get();
std::string temp;
for (unsigned int i = 0; i < len && iss.good(); ++i) {
char c = static_cast<char>(iss.get());
temp.append(1, c);
}
if (temp == "inconclusive") {
_inconclusive = true;
continue;
}
results[elem++] = temp;
if (elem == 5)
break;
}
if (elem != 5)
throw InternalError(0, "Internal Error: Deserialization of error message failed");
_id = results[0];
_severity = Severity::fromString(results[1]);
std::istringstream scwe(results[2]);
scwe >> _cwe.id;
_shortMessage = results[3];
_verboseMessage = results[4];
unsigned int stackSize = 0;
if (!(iss >> stackSize))
return false;
while (iss.good()) {
unsigned int len = 0;
if (!(iss >> len))
return false;
iss.get();
std::string temp;
for (unsigned int i = 0; i < len && iss.good(); ++i) {
const char c = static_cast<char>(iss.get());
temp.append(1, c);
}
const std::string::size_type colonPos = temp.find(':');
if (colonPos == std::string::npos)
throw InternalError(0, "Internal Error: No colon found in <filename:line> pattern");
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile(temp.substr(colonPos + 1));
temp = temp.substr(0, colonPos);
std::istringstream fiss(temp);
fiss >> loc.line;
_callStack.push_back(loc);
if (_callStack.size() >= stackSize)
break;
}
return true;
}
std::string ErrorLogger::ErrorMessage::getXMLHeader(int xml_version)
{
// xml_version 1 is the default xml format
tinyxml2::XMLPrinter printer;
// standard xml header
printer.PushDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
// header
printer.OpenElement("results", false);
// version 2 header
if (xml_version == 2) {
printer.PushAttribute("version", xml_version);
printer.OpenElement("cppcheck", false);
printer.PushAttribute("version", CppCheck::version());
printer.CloseElement(false);
printer.OpenElement("errors", false);
}
return std::string(printer.CStr()) + '>';
}
std::string ErrorLogger::ErrorMessage::getXMLFooter(int xml_version)
{
return (xml_version<=1) ? "</results>" : " </errors>\n</results>";
}
// There is no utf-8 support around but the strings should at least be safe for to tinyxml2.
// See #5300 "Invalid encoding in XML output" and #6431 "Invalid XML created - Invalid encoding of string literal "
std::string ErrorLogger::ErrorMessage::fixInvalidChars(const std::string& raw)
{
std::string result;
result.reserve(raw.length());
std::string::const_iterator from=raw.begin();
while (from!=raw.end()) {
if (std::isprint(static_cast<unsigned char>(*from))) {
result.push_back(*from);
} else {
std::ostringstream es;
// straight cast to (unsigned) doesn't work out.
const unsigned uFrom = (unsigned char)*from;
es << '\\' << std::setbase(8) << std::setw(3) << std::setfill('0') << uFrom;
result += es.str();
}
++from;
}
return result;
}
std::string ErrorLogger::ErrorMessage::toXML(bool verbose, int version) const
{
// The default xml format
if (version == 1) {
// No inconclusive messages in the xml version 1
if (_inconclusive)
return "";
tinyxml2::XMLPrinter printer(0, false, 1);
printer.OpenElement("error", false);
if (!_callStack.empty()) {
printer.PushAttribute("file", _callStack.back().getfile().c_str());
printer.PushAttribute("line", _callStack.back().line);
}
printer.PushAttribute("id", _id.c_str());
printer.PushAttribute("severity", (_severity == Severity::error ? "error" : "style"));
printer.PushAttribute("msg", fixInvalidChars(verbose ? _verboseMessage : _shortMessage).c_str());
printer.CloseElement(false);
return printer.CStr();
}
// The xml format you get when you use --xml-version=2
else if (version == 2) {
tinyxml2::XMLPrinter printer(0, false, 2);
printer.OpenElement("error", false);
printer.PushAttribute("id", _id.c_str());
printer.PushAttribute("severity", Severity::toString(_severity).c_str());
printer.PushAttribute("msg", fixInvalidChars(_shortMessage).c_str());
printer.PushAttribute("verbose", fixInvalidChars(_verboseMessage).c_str());
if (_cwe.id)
printer.PushAttribute("cwe", _cwe.id);
if (_inconclusive)
printer.PushAttribute("inconclusive", "true");
for (std::list<FileLocation>::const_reverse_iterator it = _callStack.rbegin(); it != _callStack.rend(); ++it) {
printer.OpenElement("location", false);
if (!file0.empty() && (*it).getfile() != file0)
printer.PushAttribute("file0", Path::toNativeSeparators(file0).c_str());
printer.PushAttribute("file", (*it).getfile().c_str());
printer.PushAttribute("line", (*it).line);
printer.CloseElement(false);
}
printer.CloseElement(false);
return printer.CStr();
}
return "";
}
void ErrorLogger::ErrorMessage::findAndReplace(std::string &source, const std::string &searchFor, const std::string &replaceWith)
{
std::string::size_type index = 0;
while ((index = source.find(searchFor, index)) != std::string::npos) {
source.replace(index, searchFor.length(), replaceWith);
index += replaceWith.length();
}
}
std::string ErrorLogger::ErrorMessage::toString(bool verbose, const std::string &outputFormat) const
{
// Save this ErrorMessage in plain text.
// No template is given
if (outputFormat.length() == 0) {
std::ostringstream text;
if (!_callStack.empty())
text << callStackToString(_callStack) << ": ";
if (_severity != Severity::none) {
text << '(' << Severity::toString(_severity);
if (_inconclusive)
text << ", inconclusive";
text << ") ";
}
text << (verbose ? _verboseMessage : _shortMessage);
return text.str();
}
// template is given. Reformat the output according to it
else {
std::string result = outputFormat;
// Support a few special characters to allow to specific formatting, see http://sourceforge.net/apps/phpbb/cppcheck/viewtopic.php?f=4&t=494&sid=21715d362c0dbafd3791da4d9522f814
// Substitution should be done first so messages from cppcheck never get translated.
findAndReplace(result, "\\b", "\b");
findAndReplace(result, "\\n", "\n");
findAndReplace(result, "\\r", "\r");
findAndReplace(result, "\\t", "\t");
findAndReplace(result, "{id}", _id);
findAndReplace(result, "{severity}", Severity::toString(_severity));
findAndReplace(result, "{message}", verbose ? _verboseMessage : _shortMessage);
findAndReplace(result, "{callstack}", _callStack.empty() ? "" : callStackToString(_callStack));
if (!_callStack.empty()) {
std::ostringstream oss;
oss << _callStack.back().line;
findAndReplace(result, "{line}", oss.str());
findAndReplace(result, "{file}", _callStack.back().getfile());
} else {
findAndReplace(result, "{file}", "");
findAndReplace(result, "{line}", "");
}
return result;
}
}
void ErrorLogger::reportUnmatchedSuppressions(const std::list<Suppressions::SuppressionEntry> &unmatched)
{
// Report unmatched suppressions
for (std::list<Suppressions::SuppressionEntry>::const_iterator i = unmatched.begin(); i != unmatched.end(); ++i) {
// don't report "unmatchedSuppression" as unmatched
if (i->id == "unmatchedSuppression")
continue;
// check if this unmatched suppression is suppressed
bool suppressed = false;
for (std::list<Suppressions::SuppressionEntry>::const_iterator i2 = unmatched.begin(); i2 != unmatched.end(); ++i2) {
if (i2->id == "unmatchedSuppression") {
if ((i2->file == "*" || i2->file == i->file) &&
(i2->line == 0 || i2->line == i->line)) {
suppressed = true;
break;
}
}
}
if (suppressed)
continue;
const std::list<ErrorLogger::ErrorMessage::FileLocation> callStack = make_container< std::list<ErrorLogger::ErrorMessage::FileLocation> > ()
<< ErrorLogger::ErrorMessage::FileLocation(i->file, i->line);
reportErr(ErrorLogger::ErrorMessage(callStack, emptyString, Severity::information, "Unmatched suppression: " + i->id, "unmatchedSuppression", false));
}
}
std::string ErrorLogger::callStackToString(const std::list<ErrorLogger::ErrorMessage::FileLocation> &callStack)
{
std::ostringstream ostr;
for (std::list<ErrorLogger::ErrorMessage::FileLocation>::const_iterator tok = callStack.begin(); tok != callStack.end(); ++tok) {
ostr << (tok == callStack.begin() ? "" : " -> ") << tok->stringify();
}
return ostr.str();
}
ErrorLogger::ErrorMessage::FileLocation::FileLocation(const Token* tok, const TokenList* list)
: line(tok->linenr()), _file(list->file(tok))
{
}
std::string ErrorLogger::ErrorMessage::FileLocation::getfile(bool convert) const
{
if (convert)
return Path::toNativeSeparators(_file);
return _file;
}
void ErrorLogger::ErrorMessage::FileLocation::setfile(const std::string &file)
{
_file = file;
_file = Path::fromNativeSeparators(_file);
_file = Path::simplifyPath(_file);
}
std::string ErrorLogger::ErrorMessage::FileLocation::stringify() const
{
std::ostringstream oss;
oss << '[' << Path::toNativeSeparators(_file);
if (line != 0)
oss << ':' << line;
oss << ']';
return oss.str();
}
std::string ErrorLogger::toxml(const std::string &str)
{
std::ostringstream xml;
const bool isstring(str[0] == '\"');
for (std::size_t i = 0U; i < str.length(); i++) {
char c = str[i];
switch (c) {
case '<':
xml << "<";
break;
case '>':
xml << ">";
break;
case '&':
xml << "&";
break;
case '\"':
xml << """;
break;
case '\0':
xml << "\\0";
break;
default:
if (!isstring || (c >= ' ' && c <= 'z'))
xml << c;
else
xml << 'x';
break;
}
}
return xml.str();
}