forked from danmar/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuppressions.cpp
667 lines (584 loc) · 23.2 KB
/
suppressions.cpp
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2024 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 "suppressions.h"
#include "errorlogger.h"
#include "errortypes.h"
#include "filesettings.h"
#include "path.h"
#include "utils.h"
#include "token.h"
#include "tokenize.h"
#include "tokenlist.h"
#include <algorithm>
#include <cctype> // std::isdigit, std::isalnum, etc
#include <cstring>
#include <functional> // std::bind, std::placeholders
#include <sstream>
#include <utility>
#include "xml.h"
static const char ID_UNUSEDFUNCTION[] = "unusedFunction";
static const char ID_CHECKERSREPORT[] = "checkersReport";
SuppressionList::ErrorMessage SuppressionList::ErrorMessage::fromErrorMessage(const ::ErrorMessage &msg, const std::set<std::string> ¯oNames)
{
SuppressionList::ErrorMessage ret;
ret.hash = msg.hash;
ret.errorId = msg.id;
if (!msg.callStack.empty()) {
ret.setFileName(msg.callStack.back().getfile(false));
ret.lineNumber = msg.callStack.back().line;
} else {
ret.lineNumber = SuppressionList::Suppression::NO_LINE;
}
ret.certainty = msg.certainty;
ret.symbolNames = msg.symbolNames();
ret.macroNames = macroNames;
return ret;
}
static bool isAcceptedErrorIdChar(char c)
{
switch (c) {
case '_':
case '-':
case '.':
case '*':
return true;
default:
return c > 0 && std::isalnum(c);
}
}
std::string SuppressionList::parseFile(std::istream &istr)
{
// Change '\r' to '\n' in the istr
std::string filedata;
std::string line;
while (std::getline(istr, line))
filedata += line + "\n";
std::replace(filedata.begin(), filedata.end(), '\r', '\n');
// Parse filedata..
std::istringstream istr2(filedata);
while (std::getline(istr2, line)) {
// Skip empty lines
if (line.empty())
continue;
std::string::size_type pos = 0;
while (pos < line.size() && std::isspace(line[pos]))
++pos;
if (pos == line.size())
continue;
// Skip comments
if (line[pos] == '#')
continue;
if (pos < line.size() - 1 && line[pos] == '/' && line[pos + 1] == '/')
continue;
const std::string errmsg(addSuppressionLine(line));
if (!errmsg.empty())
return errmsg;
}
return "";
}
std::string SuppressionList::parseXmlFile(const char *filename)
{
tinyxml2::XMLDocument doc;
const tinyxml2::XMLError error = doc.LoadFile(filename);
if (error != tinyxml2::XML_SUCCESS)
return std::string("failed to load suppressions XML '") + filename + "' (" + tinyxml2::XMLDocument::ErrorIDToName(error) + ").";
const tinyxml2::XMLElement * const rootnode = doc.FirstChildElement();
if (!rootnode)
return std::string("failed to load suppressions XML '") + filename + "' (no root node found).";
// TODO: check for proper root node 'suppressions'
for (const tinyxml2::XMLElement * e = rootnode->FirstChildElement(); e; e = e->NextSiblingElement()) {
if (std::strcmp(e->Name(), "suppress") != 0)
return std::string("invalid suppression xml file '") + filename + "', expected 'suppress' element but got a '" + e->Name() + "'.";
Suppression s;
for (const tinyxml2::XMLElement * e2 = e->FirstChildElement(); e2; e2 = e2->NextSiblingElement()) {
const char *name = e2->Name();
const char *text = empty_if_null(e2->GetText());
if (std::strcmp(name, "id") == 0)
s.errorId = text;
else if (std::strcmp(name, "fileName") == 0)
s.fileName = text;
else if (std::strcmp(name, "lineNumber") == 0)
s.lineNumber = strToInt<int>(text);
else if (std::strcmp(name, "symbolName") == 0)
s.symbolName = text;
else if (*text && std::strcmp(name, "hash") == 0)
s.hash = strToInt<std::size_t>(text);
else
return std::string("unknown element '") + name + "' in suppressions XML '" + filename + "', expected id/fileName/lineNumber/symbolName/hash.";
}
const std::string err = addSuppression(std::move(s));
if (!err.empty())
return err;
}
return "";
}
std::vector<SuppressionList::Suppression> SuppressionList::parseMultiSuppressComment(const std::string &comment, std::string *errorMessage)
{
std::vector<Suppression> suppressions;
// If this function is called we assume that comment starts with "cppcheck-suppress[".
const std::string::size_type start_position = comment.find('[');
const std::string::size_type end_position = comment.find(']', start_position);
if (end_position == std::string::npos) {
if (errorMessage && errorMessage->empty())
*errorMessage = "Bad multi suppression '" + comment + "'. legal format is cppcheck-suppress[errorId, errorId symbolName=arr, ...]";
return suppressions;
}
// parse all suppressions
for (std::string::size_type pos = start_position; pos < end_position;) {
const std::string::size_type pos1 = pos + 1;
pos = comment.find(',', pos1);
const std::string::size_type pos2 = (pos < end_position) ? pos : end_position;
if (pos1 == pos2)
continue;
Suppression s;
std::istringstream iss(comment.substr(pos1, pos2-pos1));
iss >> s.errorId;
if (!iss) {
if (errorMessage && errorMessage->empty())
*errorMessage = "Bad multi suppression '" + comment + "'. legal format is cppcheck-suppress[errorId, errorId symbolName=arr, ...]";
suppressions.clear();
return suppressions;
}
const std::string symbolNameString = "symbolName=";
while (iss) {
std::string word;
iss >> word;
if (!iss)
break;
if (word.find_first_not_of("+-*/%#;") == std::string::npos)
break;
if (startsWith(word, symbolNameString)) {
s.symbolName = word.substr(symbolNameString.size());
} else {
if (errorMessage && errorMessage->empty())
*errorMessage = "Bad multi suppression '" + comment + "'. legal format is cppcheck-suppress[errorId, errorId symbolName=arr, ...]";
suppressions.clear();
return suppressions;
}
}
suppressions.push_back(std::move(s));
}
return suppressions;
}
SuppressionList::Suppression SuppressionList::parseLine(const std::string &line)
{
std::istringstream lineStream;
SuppressionList::Suppression suppression;
// Strip any end of line comments
std::string::size_type endpos = std::min(line.find('#'), line.find("//"));
if (endpos != std::string::npos) {
while (endpos > 0 && std::isspace(line[endpos-1])) {
endpos--;
}
lineStream.str(line.substr(0, endpos));
} else {
lineStream.str(line);
}
if (std::getline(lineStream, suppression.errorId, ':')) {
if (std::getline(lineStream, suppression.fileName)) {
// If there is not a dot after the last colon in "file" then
// the colon is a separator and the contents after the colon
// is a line number..
// Get position of last colon
const std::string::size_type pos = suppression.fileName.rfind(':');
// if a colon is found and there is no dot after it..
if (pos != std::string::npos &&
suppression.fileName.find('.', pos) == std::string::npos) {
// Try to parse out the line number
try {
std::istringstream istr1(suppression.fileName.substr(pos+1));
istr1 >> suppression.lineNumber;
} catch (...) {
suppression.lineNumber = SuppressionList::Suppression::NO_LINE;
}
if (suppression.lineNumber != SuppressionList::Suppression::NO_LINE) {
suppression.fileName.erase(pos);
}
}
}
}
suppression.fileName = Path::simplifyPath(suppression.fileName);
return suppression;
}
std::string SuppressionList::addSuppressionLine(const std::string &line)
{
return addSuppression(parseLine(line));
}
std::string SuppressionList::addSuppression(SuppressionList::Suppression suppression)
{
std::lock_guard<std::mutex> lg(mSuppressionsSync);
// Check if suppression is already in list
auto foundSuppression = std::find_if(mSuppressions.begin(), mSuppressions.end(),
std::bind(&Suppression::isSameParameters, &suppression, std::placeholders::_1));
if (foundSuppression != mSuppressions.end()) {
return "suppression '" + suppression.toString() + "' already exists";
}
// Check that errorId is valid..
if (suppression.errorId.empty() && suppression.hash == 0)
return "Failed to add suppression. No id.";
for (std::string::size_type pos = 0; pos < suppression.errorId.length(); ++pos) {
if (!isAcceptedErrorIdChar(suppression.errorId[pos])) {
return "Failed to add suppression. Invalid id \"" + suppression.errorId + "\"";
}
if (pos == 0 && std::isdigit(suppression.errorId[pos])) {
return "Failed to add suppression. Invalid id \"" + suppression.errorId + "\"";
}
}
if (!isValidGlobPattern(suppression.errorId))
return "Failed to add suppression. Invalid glob pattern '" + suppression.errorId + "'.";
if (!isValidGlobPattern(suppression.fileName))
return "Failed to add suppression. Invalid glob pattern '" + suppression.fileName + "'.";
mSuppressions.push_back(std::move(suppression));
return "";
}
std::string SuppressionList::addSuppressions(std::list<Suppression> suppressions)
{
for (auto &newSuppression : suppressions) {
auto errmsg = addSuppression(std::move(newSuppression));
if (!errmsg.empty())
return errmsg;
}
return "";
}
// cppcheck-suppress unusedFunction
bool SuppressionList::updateSuppressionState(const SuppressionList::Suppression& suppression)
{
std::lock_guard<std::mutex> lg(mSuppressionsSync);
// Check if suppression is already in list
auto foundSuppression = std::find_if(mSuppressions.begin(), mSuppressions.end(),
std::bind(&Suppression::isSameParameters, &suppression, std::placeholders::_1));
if (foundSuppression != mSuppressions.end()) {
// Update matched state of existing global suppression
if (!suppression.isLocal() && suppression.matched)
foundSuppression->matched = suppression.matched;
return true;
}
return false;
}
void SuppressionList::ErrorMessage::setFileName(std::string s)
{
mFileName = Path::simplifyPath(std::move(s));
}
bool SuppressionList::Suppression::parseComment(std::string comment, std::string *errorMessage)
{
if (comment.size() < 2)
return false;
if (comment.find(';') != std::string::npos)
comment.erase(comment.find(';'));
if (comment.find("//", 2) != std::string::npos)
comment.erase(comment.find("//",2));
if (comment.compare(comment.size() - 2, 2, "*/") == 0)
comment.erase(comment.size() - 2, 2);
const std::set<std::string> cppchecksuppress{
"cppcheck-suppress",
"cppcheck-suppress-begin",
"cppcheck-suppress-end",
"cppcheck-suppress-file",
"cppcheck-suppress-macro"
};
std::istringstream iss(comment.substr(2));
std::string word;
iss >> word;
if (!cppchecksuppress.count(word))
return false;
iss >> errorId;
if (!iss)
return false;
const std::string symbolNameString = "symbolName=";
while (iss) {
iss >> word;
if (!iss)
break;
if (word.find_first_not_of("+-*/%#;") == std::string::npos)
break;
if (startsWith(word, symbolNameString))
symbolName = word.substr(symbolNameString.size());
else if (errorMessage && errorMessage->empty())
*errorMessage = "Bad suppression attribute '" + word + "'. You can write comments in the comment after a ; or //. Valid suppression attributes; symbolName=sym";
}
return true;
}
bool SuppressionList::Suppression::isSuppressed(const SuppressionList::ErrorMessage &errmsg) const
{
if (hash > 0 && hash != errmsg.hash)
return false;
if (!errorId.empty() && !matchglob(errorId, errmsg.errorId))
return false;
if (type == SuppressionList::Type::macro) {
if (errmsg.macroNames.count(macroName) == 0)
return false;
} else {
if (!fileName.empty() && !matchglob(fileName, errmsg.getFileName()))
return false;
if ((SuppressionList::Type::unique == type) && (lineNumber != NO_LINE) && (lineNumber != errmsg.lineNumber)) {
if (!thisAndNextLine || lineNumber + 1 != errmsg.lineNumber)
return false;
}
if ((SuppressionList::Type::block == type) && ((errmsg.lineNumber < lineBegin) || (errmsg.lineNumber > lineEnd)))
return false;
}
if (!symbolName.empty()) {
for (std::string::size_type pos = 0; pos < errmsg.symbolNames.size();) {
const std::string::size_type pos2 = errmsg.symbolNames.find('\n',pos);
std::string symname;
if (pos2 == std::string::npos) {
symname = errmsg.symbolNames.substr(pos);
pos = pos2;
} else {
symname = errmsg.symbolNames.substr(pos,pos2-pos);
pos = pos2+1;
}
if (matchglob(symbolName, symname))
return true;
}
return false;
}
return true;
}
bool SuppressionList::Suppression::isMatch(const SuppressionList::ErrorMessage &errmsg)
{
if (!isSuppressed(errmsg))
return false;
matched = true;
checked = true;
return true;
}
// cppcheck-suppress unusedFunction - used by GUI only
std::string SuppressionList::Suppression::getText() const
{
std::string ret;
if (!errorId.empty())
ret = errorId;
if (!fileName.empty())
ret += " fileName=" + fileName;
if (lineNumber != NO_LINE)
ret += " lineNumber=" + std::to_string(lineNumber);
if (!symbolName.empty())
ret += " symbolName=" + symbolName;
if (hash > 0)
ret += " hash=" + std::to_string(hash);
if (startsWith(ret," "))
return ret.substr(1);
return ret;
}
bool SuppressionList::isSuppressed(const SuppressionList::ErrorMessage &errmsg, bool global)
{
std::lock_guard<std::mutex> lg(mSuppressionsSync);
const bool unmatchedSuppression(errmsg.errorId == "unmatchedSuppression");
bool returnValue = false;
for (Suppression &s : mSuppressions) {
if (!global && !s.isLocal())
continue;
if (unmatchedSuppression && s.errorId != errmsg.errorId)
continue;
if (s.isMatch(errmsg))
returnValue = true;
}
return returnValue;
}
bool SuppressionList::isSuppressedExplicitly(const SuppressionList::ErrorMessage &errmsg, bool global)
{
std::lock_guard<std::mutex> lg(mSuppressionsSync);
for (Suppression &s : mSuppressions) {
if (!global && !s.isLocal())
continue;
if (s.errorId != errmsg.errorId) // Error id must match exactly
continue;
if (s.isMatch(errmsg))
return true;
}
return false;
}
bool SuppressionList::isSuppressed(const ::ErrorMessage &errmsg, const std::set<std::string>& macroNames)
{
{
std::lock_guard<std::mutex> lg(mSuppressionsSync);
if (mSuppressions.empty())
return false;
}
return isSuppressed(SuppressionList::ErrorMessage::fromErrorMessage(errmsg, macroNames));
}
void SuppressionList::dump(std::ostream & out) const
{
std::lock_guard<std::mutex> lg(mSuppressionsSync);
out << " <suppressions>" << std::endl;
for (const Suppression &suppression : mSuppressions) {
out << " <suppression";
out << " errorId=\"" << ErrorLogger::toxml(suppression.errorId) << '"';
if (!suppression.fileName.empty())
out << " fileName=\"" << ErrorLogger::toxml(suppression.fileName) << '"';
if (suppression.lineNumber != Suppression::NO_LINE)
out << " lineNumber=\"" << suppression.lineNumber << '"';
if (!suppression.symbolName.empty())
out << " symbolName=\"" << ErrorLogger::toxml(suppression.symbolName) << '\"';
if (suppression.hash > 0)
out << " hash=\"" << suppression.hash << '\"';
if (suppression.lineBegin != Suppression::NO_LINE)
out << " lineBegin=\"" << suppression.lineBegin << '"';
if (suppression.lineEnd != Suppression::NO_LINE)
out << " lineEnd=\"" << suppression.lineEnd << '"';
if (suppression.type == SuppressionList::Type::file)
out << " type=\"file\"";
else if (suppression.type == SuppressionList::Type::block)
out << " type=\"block\"";
else if (suppression.type == SuppressionList::Type::blockBegin)
out << " type=\"blockBegin\"";
else if (suppression.type == SuppressionList::Type::blockEnd)
out << " type=\"blockEnd\"";
else if (suppression.type == SuppressionList::Type::macro)
out << " type=\"macro\"";
out << " />" << std::endl;
}
out << " </suppressions>" << std::endl;
}
std::list<SuppressionList::Suppression> SuppressionList::getUnmatchedLocalSuppressions(const FileWithDetails &file, const bool includeUnusedFunction) const
{
std::lock_guard<std::mutex> lg(mSuppressionsSync);
std::list<Suppression> result;
for (const Suppression &s : mSuppressions) {
if (s.isInline)
continue;
if (s.matched || ((s.lineNumber != Suppression::NO_LINE) && !s.checked))
continue;
if (s.type == SuppressionList::Type::macro)
continue;
if (s.hash > 0)
continue;
if (s.errorId == ID_CHECKERSREPORT)
continue;
if (!includeUnusedFunction && s.errorId == ID_UNUSEDFUNCTION)
continue;
if (!s.isLocal() || s.fileName != file.spath())
continue;
result.push_back(s);
}
return result;
}
std::list<SuppressionList::Suppression> SuppressionList::getUnmatchedGlobalSuppressions(const bool includeUnusedFunction) const
{
std::lock_guard<std::mutex> lg(mSuppressionsSync);
std::list<Suppression> result;
for (const Suppression &s : mSuppressions) {
if (s.isInline)
continue;
if (s.matched || ((s.lineNumber != Suppression::NO_LINE) && !s.checked))
continue;
if (s.hash > 0)
continue;
if (!includeUnusedFunction && s.errorId == ID_UNUSEDFUNCTION)
continue;
if (s.errorId == ID_CHECKERSREPORT)
continue;
if (s.isLocal())
continue;
result.push_back(s);
}
return result;
}
std::list<SuppressionList::Suppression> SuppressionList::getUnmatchedInlineSuppressions() const
{
std::list<SuppressionList::Suppression> result;
for (const SuppressionList::Suppression &s : SuppressionList::mSuppressions) {
if (!s.isInline)
continue;
if (!s.checked)
continue;
if (s.matched)
continue;
if (s.hash > 0)
continue;
result.push_back(s);
}
return result;
}
std::list<SuppressionList::Suppression> SuppressionList::getSuppressions() const
{
std::lock_guard<std::mutex> lg(mSuppressionsSync);
return mSuppressions;
}
void SuppressionList::markUnmatchedInlineSuppressionsAsChecked(const Tokenizer &tokenizer) {
std::lock_guard<std::mutex> lg(mSuppressionsSync);
int currLineNr = -1;
int currFileIdx = -1;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) {
if (currFileIdx != tok->fileIndex() || currLineNr != tok->linenr()) {
currLineNr = tok->linenr();
currFileIdx = tok->fileIndex();
for (auto &suppression : mSuppressions) {
if (suppression.type == SuppressionList::Type::unique) {
if (!suppression.checked && (suppression.lineNumber == currLineNr) && (suppression.fileName == tokenizer.list.file(tok))) {
suppression.checked = true;
}
} else if (suppression.type == SuppressionList::Type::block) {
if ((!suppression.checked && (suppression.lineBegin <= currLineNr) && (suppression.lineEnd >= currLineNr) && (suppression.fileName == tokenizer.list.file(tok)))) {
suppression.checked = true;
}
} else if (!suppression.checked && suppression.fileName == tokenizer.list.file(tok)) {
suppression.checked = true;
}
}
}
}
}
bool SuppressionList::reportUnmatchedSuppressions(const std::list<SuppressionList::Suppression> &unmatched, ErrorLogger &errorLogger)
{
bool err = false;
// Report unmatched suppressions
for (const SuppressionList::Suppression &s : unmatched) {
// don't report "unmatchedSuppression" as unmatched
if (s.errorId == "unmatchedSuppression")
continue;
// check if this unmatched suppression is suppressed
bool suppressed = false;
for (const SuppressionList::Suppression &s2 : unmatched) {
if (s2.errorId == "unmatchedSuppression") {
if ((s2.fileName.empty() || s2.fileName == "*" || s2.fileName == s.fileName) &&
(s2.lineNumber == SuppressionList::Suppression::NO_LINE || s2.lineNumber == s.lineNumber)) {
suppressed = true;
break;
}
}
}
if (suppressed)
continue;
std::list<::ErrorMessage::FileLocation> callStack;
if (!s.fileName.empty())
callStack.emplace_back(s.fileName, s.lineNumber, 0);
errorLogger.reportErr(::ErrorMessage(std::move(callStack), "", Severity::information, "Unmatched suppression: " + s.errorId, "unmatchedSuppression", Certainty::normal));
err = true;
}
return err;
}
std::string SuppressionList::Suppression::toString() const
{
std::string s;
s+= errorId;
if (!fileName.empty()) {
s += ':';
s += fileName;
if (lineNumber != -1) {
s += ':';
s += std::to_string(lineNumber);
}
}
if (!symbolName.empty()) {
s += ':';
s += symbolName;
}
return s;
}