2011-08-22 18:54:23 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
|
|
|
* Copyright (C) 2007-2011 Daniel Marjamäki and 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 "settings.h"
|
|
|
|
#include "path.h"
|
|
|
|
|
|
|
|
#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";
|
|
|
|
while (filedata.find("\r") != std::string::npos)
|
|
|
|
filedata[filedata.find("\r")] = '\n';
|
|
|
|
|
|
|
|
// 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
|
|
|
|
const std::string::size_type pos = file.rfind(":");
|
|
|
|
|
|
|
|
// if a colon is found and there is no dot after it..
|
|
|
|
if (pos != std::string::npos &&
|
2011-10-13 20:53:06 +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
|
|
|
|
const std::string errmsg(addSuppression(id, file, lineNumber));
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no other paths to tray, 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 {
|
2011-08-22 18:54:23 +02:00
|
|
|
_files[Path::simplifyPath(name.c_str())][line] = false;
|
|
|
|
}
|
|
|
|
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)) {
|
|
|
|
if (g->second.find(0U) != g->second.end()) {
|
2011-08-22 18:54:23 +02:00
|
|
|
g->second[0U] = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
std::map<unsigned int, bool>::iterator 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()) {
|
|
|
|
if (f->second.find(0U) != f->second.end()) {
|
2011-08-22 18:54:23 +02:00
|
|
|
f->second[0U] = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
std::map<unsigned int, bool>::iterator 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 != "*") {
|
|
|
|
for (std::string::size_type pos = 0; pos < errorId.length(); ++pos) {
|
|
|
|
if (errorId[pos] < 0 || !std::isalnum(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;
|
|
|
|
|
|
|
|
if (_suppressions.find(errorId) == _suppressions.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return _suppressions[errorId].isSuppressed(file, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (_suppressions.find(errorId) == _suppressions.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return _suppressions[errorId].isSuppressedLocal(file, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::list<Suppressions::SuppressionEntry> Suppressions::getUnmatchedLocalSuppressions(const std::string &file) const
|
|
|
|
{
|
|
|
|
std::list<SuppressionEntry> r;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (std::map<std::string, FileMatcher>::const_iterator i = _suppressions.begin(); i != _suppressions.end(); ++i) {
|
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) {
|
2011-08-22 18:54:23 +02:00
|
|
|
r.push_back(SuppressionEntry(i->first, f->first, l->first));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::list<Suppressions::SuppressionEntry> Suppressions::getUnmatchedGlobalSuppressions() const
|
|
|
|
{
|
|
|
|
std::list<SuppressionEntry> r;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (std::map<std::string, FileMatcher>::const_iterator i = _suppressions.begin(); i != _suppressions.end(); ++i) {
|
|
|
|
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) {
|
2011-08-22 18:54:23 +02:00
|
|
|
r.push_back(SuppressionEntry(i->first, g->first, l->first));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|