2011-08-22 18:54:23 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2015-11-18 20:04:50 +01:00
|
|
|
* Copyright (C) 2007-2015 Cppcheck team.
|
2011-08-22 18:54:23 +02:00
|
|
|
*
|
|
|
|
* 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 "settings.h"
|
|
|
|
#include "path.h"
|
|
|
|
|
2012-07-24 21:21:05 +02:00
|
|
|
#include <algorithm>
|
2011-08-22 18:54:23 +02:00
|
|
|
#include <sstream>
|
|
|
|
#include <stack>
|
2011-08-22 19:21:25 +02:00
|
|
|
#include <cctype> // std::isdigit, std::isalnum, etc
|
2011-08-22 18:54:23 +02:00
|
|
|
|
|
|
|
std::string Suppressions::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";
|
2012-07-24 21:21:05 +02:00
|
|
|
std::replace(filedata.begin(), filedata.end(), '\r', '\n');
|
2011-08-22 18:54:23 +02:00
|
|
|
|
|
|
|
// Parse filedata..
|
|
|
|
std::istringstream istr2(filedata);
|
2011-10-13 20:53:06 +02:00
|
|
|
while (std::getline(istr2, line)) {
|
2011-08-22 18:54:23 +02:00
|
|
|
// Skip empty lines
|
|
|
|
if (line.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Skip comments
|
|
|
|
if (line.length() >= 2 && line[0] == '/' && line[1] == '/')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const std::string errmsg(addSuppressionLine(line));
|
|
|
|
if (!errmsg.empty())
|
|
|
|
return errmsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Suppressions::addSuppressionLine(const std::string &line)
|
|
|
|
{
|
|
|
|
std::istringstream lineStream(line);
|
|
|
|
std::string id;
|
|
|
|
std::string file;
|
|
|
|
unsigned int lineNumber = 0;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (std::getline(lineStream, id, ':')) {
|
|
|
|
if (std::getline(lineStream, file)) {
|
2011-08-22 18:54:23 +02:00
|
|
|
// 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
|
2015-08-15 20:24:26 +02:00
|
|
|
const std::string::size_type pos = file.rfind(':');
|
2011-08-22 18:54:23 +02:00
|
|
|
|
|
|
|
// if a colon is found and there is no dot after it..
|
|
|
|
if (pos != std::string::npos &&
|
2015-08-15 20:24:26 +02:00
|
|
|
file.find('.', pos) == std::string::npos) {
|
2011-08-22 18:54:23 +02:00
|
|
|
// Try to parse out the line number
|
2011-10-13 20:53:06 +02:00
|
|
|
try {
|
2011-08-22 18:54:23 +02:00
|
|
|
std::istringstream istr1(file.substr(pos+1));
|
|
|
|
istr1 >> lineNumber;
|
2011-10-13 20:53:06 +02:00
|
|
|
} catch (...) {
|
2011-08-22 18:54:23 +02:00
|
|
|
lineNumber = 0;
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (lineNumber > 0) {
|
2011-08-22 18:54:23 +02:00
|
|
|
file.erase(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We could perhaps check if the id is valid and return error if it is not
|
2013-03-12 15:53:18 +01:00
|
|
|
const std::string errmsg(addSuppression(id, Path::fromNativeSeparators(file), lineNumber));
|
2011-08-22 18:54:23 +02:00
|
|
|
if (!errmsg.empty())
|
|
|
|
return errmsg;
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Suppressions::FileMatcher::match(const std::string &pattern, const std::string &name)
|
|
|
|
{
|
|
|
|
const char *p = pattern.c_str();
|
|
|
|
const char *n = name.c_str();
|
|
|
|
std::stack<std::pair<const char *, const char *> > backtrack;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (;;) {
|
2011-08-22 18:54:23 +02:00
|
|
|
bool matching = true;
|
2011-10-13 20:53:06 +02:00
|
|
|
while (*p != '\0' && matching) {
|
|
|
|
switch (*p) {
|
2011-08-22 18:54:23 +02:00
|
|
|
case '*':
|
|
|
|
// Step forward until we match the next character after *
|
2011-10-13 20:53:06 +02:00
|
|
|
while (*n != '\0' && *n != p[1]) {
|
2011-08-22 18:54:23 +02:00
|
|
|
n++;
|
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
if (*n != '\0') {
|
2011-08-22 18:54:23 +02:00
|
|
|
// If this isn't the last possibility, save it for later
|
|
|
|
backtrack.push(std::make_pair(p, n));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
// Any character matches unless we're at the end of the name
|
2011-10-13 20:53:06 +02:00
|
|
|
if (*n != '\0') {
|
2011-08-22 18:54:23 +02:00
|
|
|
n++;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2011-08-22 18:54:23 +02:00
|
|
|
matching = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Non-wildcard characters match literally
|
2011-10-13 20:53:06 +02:00
|
|
|
if (*n == *p) {
|
2011-08-22 18:54:23 +02:00
|
|
|
n++;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2011-08-22 18:54:23 +02:00
|
|
|
matching = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we haven't failed matching and we've reached the end of the name, then success
|
2011-10-13 20:53:06 +02:00
|
|
|
if (matching && *n == '\0') {
|
2011-08-22 18:54:23 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-10 07:43:09 +01:00
|
|
|
// If there are no other paths to try, then fail
|
2011-10-13 20:53:06 +02:00
|
|
|
if (backtrack.empty()) {
|
2011-08-22 18:54:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore pointers from backtrack stack
|
|
|
|
p = backtrack.top().first;
|
|
|
|
n = backtrack.top().second;
|
|
|
|
backtrack.pop();
|
|
|
|
|
|
|
|
// Advance name pointer by one because the current position didn't work
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Suppressions::FileMatcher::addFile(const std::string &name, unsigned int line)
|
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (name.find_first_of("*?") != std::string::npos) {
|
|
|
|
for (std::string::const_iterator i = name.begin(); i != name.end(); ++i) {
|
|
|
|
if (*i == '*') {
|
2011-08-22 18:54:23 +02:00
|
|
|
std::string::const_iterator j = i + 1;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (j != name.end() && (*j == '*' || *j == '?')) {
|
2011-08-22 18:54:23 +02:00
|
|
|
return "Failed to add suppression. Syntax error in glob.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_globs[name][line] = false;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else if (name.empty()) {
|
2011-08-22 18:54:23 +02:00
|
|
|
_globs["*"][0U] = false;
|
2011-10-13 20:53:06 +02:00
|
|
|
} else {
|
2014-04-02 13:56:34 +02:00
|
|
|
_files[Path::simplifyPath(name)][line] = false;
|
2011-08-22 18:54:23 +02:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Suppressions::FileMatcher::isSuppressed(const std::string &file, unsigned int line)
|
|
|
|
{
|
|
|
|
if (isSuppressedLocal(file, line))
|
|
|
|
return true;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (std::map<std::string, std::map<unsigned int, bool> >::iterator g = _globs.begin(); g != _globs.end(); ++g) {
|
|
|
|
if (match(g->first, file)) {
|
2012-02-18 14:44:04 +01:00
|
|
|
std::map<unsigned int, bool>::iterator l = g->second.find(0U);
|
|
|
|
if (l != g->second.end()) {
|
|
|
|
l->second = true;
|
2011-08-22 18:54:23 +02:00
|
|
|
return true;
|
|
|
|
}
|
2012-02-18 14:44:04 +01:00
|
|
|
l = g->second.find(line);
|
2011-10-13 20:53:06 +02:00
|
|
|
if (l != g->second.end()) {
|
2011-08-22 18:54:23 +02:00
|
|
|
l->second = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Suppressions::FileMatcher::isSuppressedLocal(const std::string &file, unsigned int line)
|
|
|
|
{
|
|
|
|
std::map<std::string, std::map<unsigned int, bool> >::iterator f = _files.find(file);
|
2011-10-13 20:53:06 +02:00
|
|
|
if (f != _files.end()) {
|
2012-02-18 14:44:04 +01:00
|
|
|
std::map<unsigned int, bool>::iterator l = f->second.find(0U);
|
|
|
|
if (l != f->second.end()) {
|
|
|
|
l->second = true;
|
2011-08-22 18:54:23 +02:00
|
|
|
return true;
|
|
|
|
}
|
2012-02-18 14:44:04 +01:00
|
|
|
l = f->second.find(line);
|
2011-10-13 20:53:06 +02:00
|
|
|
if (l != f->second.end()) {
|
2011-08-22 18:54:23 +02:00
|
|
|
l->second = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Suppressions::addSuppression(const std::string &errorId, const std::string &file, unsigned int line)
|
|
|
|
{
|
|
|
|
// Check that errorId is valid..
|
2011-10-13 20:53:06 +02:00
|
|
|
if (errorId.empty()) {
|
2011-08-22 18:54:23 +02:00
|
|
|
return "Failed to add suppression. No id.";
|
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
if (errorId != "*") {
|
2013-02-10 07:43:09 +01:00
|
|
|
// Support "stlBoundries", as that was the name of the errorId until v1.59.
|
|
|
|
if (errorId == "stlBoundries") {
|
|
|
|
return _suppressions["stlBoundaries"].addFile(file, line);
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (std::string::size_type pos = 0; pos < errorId.length(); ++pos) {
|
2013-02-10 07:43:09 +01:00
|
|
|
if (errorId[pos] < 0 || (!std::isalnum(errorId[pos]) && errorId[pos] != '_')) {
|
2011-08-22 18:54:23 +02:00
|
|
|
return "Failed to add suppression. Invalid id \"" + errorId + "\"";
|
|
|
|
}
|
2011-10-13 20:53:06 +02:00
|
|
|
if (pos == 0 && std::isdigit(errorId[pos])) {
|
2011-08-22 18:54:23 +02:00
|
|
|
return "Failed to add suppression. Invalid id \"" + errorId + "\"";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _suppressions[errorId].addFile(file, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Suppressions::isSuppressed(const std::string &errorId, const std::string &file, unsigned int line)
|
|
|
|
{
|
|
|
|
if (errorId != "unmatchedSuppression" && _suppressions.find("*") != _suppressions.end())
|
|
|
|
if (_suppressions["*"].isSuppressed(file, line))
|
|
|
|
return true;
|
|
|
|
|
2015-08-13 15:10:23 +02:00
|
|
|
std::map<std::string, FileMatcher>::iterator suppression = _suppressions.find(errorId);
|
|
|
|
if (suppression == _suppressions.end())
|
2011-08-22 18:54:23 +02:00
|
|
|
return false;
|
|
|
|
|
2015-08-13 15:10:23 +02:00
|
|
|
return suppression->second.isSuppressed(file, line);
|
2011-08-22 18:54:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Suppressions::isSuppressedLocal(const std::string &errorId, const std::string &file, unsigned int line)
|
|
|
|
{
|
|
|
|
if (errorId != "unmatchedSuppression" && _suppressions.find("*") != _suppressions.end())
|
|
|
|
if (_suppressions["*"].isSuppressedLocal(file, line))
|
|
|
|
return true;
|
|
|
|
|
2015-08-13 15:10:23 +02:00
|
|
|
std::map<std::string, FileMatcher>::iterator suppression = _suppressions.find(errorId);
|
|
|
|
if (suppression == _suppressions.end())
|
2011-08-22 18:54:23 +02:00
|
|
|
return false;
|
|
|
|
|
2015-08-13 15:10:23 +02:00
|
|
|
return suppression->second.isSuppressedLocal(file, line);
|
2011-08-22 18:54:23 +02:00
|
|
|
}
|
|
|
|
|
2015-01-07 19:26:16 +01:00
|
|
|
std::list<Suppressions::SuppressionEntry> Suppressions::getUnmatchedLocalSuppressions(const std::string &file, const bool unusedFunctionChecking) const
|
2011-08-22 18:54:23 +02:00
|
|
|
{
|
2015-08-13 15:10:23 +02:00
|
|
|
std::list<SuppressionEntry> result;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (std::map<std::string, FileMatcher>::const_iterator i = _suppressions.begin(); i != _suppressions.end(); ++i) {
|
2015-01-07 19:26:16 +01:00
|
|
|
if (!unusedFunctionChecking && i->first == "unusedFunction")
|
|
|
|
continue;
|
2012-09-23 10:56:12 +02:00
|
|
|
|
2011-08-22 18:54:23 +02:00
|
|
|
std::map<std::string, std::map<unsigned int, bool> >::const_iterator f = i->second._files.find(file);
|
2011-10-13 20:53:06 +02:00
|
|
|
if (f != i->second._files.end()) {
|
|
|
|
for (std::map<unsigned int, bool>::const_iterator l = f->second.begin(); l != f->second.end(); ++l) {
|
|
|
|
if (!l->second) {
|
2015-08-13 15:10:23 +02:00
|
|
|
result.push_back(SuppressionEntry(i->first, f->first, l->first));
|
2011-08-22 18:54:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-13 15:10:23 +02:00
|
|
|
return result;
|
2011-08-22 18:54:23 +02:00
|
|
|
}
|
|
|
|
|
2015-01-07 19:26:16 +01:00
|
|
|
std::list<Suppressions::SuppressionEntry> Suppressions::getUnmatchedGlobalSuppressions(const bool unusedFunctionChecking) const
|
2011-08-22 18:54:23 +02:00
|
|
|
{
|
2015-08-13 15:10:23 +02:00
|
|
|
std::list<SuppressionEntry> result;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (std::map<std::string, FileMatcher>::const_iterator i = _suppressions.begin(); i != _suppressions.end(); ++i) {
|
2014-09-01 10:13:03 +02:00
|
|
|
if (!unusedFunctionChecking && i->first == "unusedFunction")
|
|
|
|
continue;
|
|
|
|
|
2012-09-23 10:56:12 +02:00
|
|
|
// global suppressions..
|
2011-10-13 20:53:06 +02:00
|
|
|
for (std::map<std::string, std::map<unsigned int, bool> >::const_iterator g = i->second._globs.begin(); g != i->second._globs.end(); ++g) {
|
|
|
|
for (std::map<unsigned int, bool>::const_iterator l = g->second.begin(); l != g->second.end(); ++l) {
|
|
|
|
if (!l->second) {
|
2015-08-13 15:10:23 +02:00
|
|
|
result.push_back(SuppressionEntry(i->first, g->first, l->first));
|
2011-08-22 18:54:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-13 15:10:23 +02:00
|
|
|
return result;
|
2011-08-22 18:54:23 +02:00
|
|
|
}
|