2011-08-22 18:54:23 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 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/>.
|
|
|
|
*/
|
2013-09-04 20:59:49 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#ifndef suppressionsH
|
|
|
|
#define suppressionsH
|
|
|
|
//---------------------------------------------------------------------------
|
2011-08-22 18:54:23 +02:00
|
|
|
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <cstddef>
|
2011-08-22 18:54:23 +02:00
|
|
|
#include <istream>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <list>
|
|
|
|
#include <string>
|
2022-09-16 18:59:15 +02:00
|
|
|
#include <utility>
|
2020-02-23 18:04:24 +01:00
|
|
|
#include <vector>
|
2011-08-22 18:54:23 +02:00
|
|
|
|
2011-08-22 19:46:53 +02:00
|
|
|
/// @addtogroup Core
|
|
|
|
/// @{
|
2011-08-22 18:54:23 +02:00
|
|
|
|
2022-03-30 19:24:53 +02:00
|
|
|
class Tokenizer;
|
2023-03-03 18:37:09 +01:00
|
|
|
class ErrorMessage;
|
2023-04-08 16:08:47 +02:00
|
|
|
enum class Certainty;
|
2022-03-30 19:24:53 +02:00
|
|
|
|
2011-08-22 18:54:23 +02:00
|
|
|
/** @brief class for handling suppressions */
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB Suppressions {
|
2018-04-09 06:43:48 +02:00
|
|
|
public:
|
|
|
|
|
|
|
|
struct CPPCHECKLIB ErrorMessage {
|
2020-07-21 11:27:03 +02:00
|
|
|
std::size_t hash;
|
2018-04-09 06:43:48 +02:00
|
|
|
std::string errorId;
|
2022-12-09 19:34:51 +01:00
|
|
|
void setFileName(std::string s);
|
2018-04-09 06:43:48 +02:00
|
|
|
const std::string &getFileName() const {
|
2018-06-17 08:19:10 +02:00
|
|
|
return mFileName;
|
2018-04-09 06:43:48 +02:00
|
|
|
}
|
|
|
|
int lineNumber;
|
2023-01-08 19:31:54 +01:00
|
|
|
Certainty certainty;
|
2018-04-09 06:43:48 +02:00
|
|
|
std::string symbolNames;
|
2011-08-22 18:54:23 +02:00
|
|
|
private:
|
2018-06-17 08:19:10 +02:00
|
|
|
std::string mFileName;
|
2018-04-09 06:43:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CPPCHECKLIB Suppression {
|
2022-03-30 19:24:53 +02:00
|
|
|
Suppression() : lineNumber(NO_LINE), hash(0), thisAndNextLine(false), matched(false), checked(false) {}
|
2018-04-09 06:43:48 +02:00
|
|
|
Suppression(const Suppression &other) {
|
|
|
|
*this = other;
|
|
|
|
}
|
2022-09-16 18:59:15 +02:00
|
|
|
Suppression(std::string id, std::string file, int line=NO_LINE) : errorId(std::move(id)), fileName(std::move(file)), lineNumber(line), hash(0), thisAndNextLine(false), matched(false), checked(false) {}
|
2018-04-09 06:43:48 +02:00
|
|
|
|
|
|
|
Suppression & operator=(const Suppression &other) {
|
|
|
|
errorId = other.errorId;
|
|
|
|
fileName = other.fileName;
|
|
|
|
lineNumber = other.lineNumber;
|
|
|
|
symbolName = other.symbolName;
|
2020-07-21 11:27:03 +02:00
|
|
|
hash = other.hash;
|
2020-10-02 18:56:26 +02:00
|
|
|
thisAndNextLine = other.thisAndNextLine;
|
2018-04-09 06:43:48 +02:00
|
|
|
matched = other.matched;
|
2022-03-30 19:24:53 +02:00
|
|
|
checked = other.checked;
|
2018-04-09 06:43:48 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const Suppression &other) const {
|
|
|
|
if (errorId != other.errorId)
|
|
|
|
return errorId < other.errorId;
|
|
|
|
if (lineNumber < other.lineNumber)
|
2018-10-04 17:13:11 +02:00
|
|
|
return true;
|
2018-04-09 06:43:48 +02:00
|
|
|
if (fileName != other.fileName)
|
|
|
|
return fileName < other.fileName;
|
|
|
|
if (symbolName != other.symbolName)
|
|
|
|
return symbolName < other.symbolName;
|
2020-07-21 11:27:03 +02:00
|
|
|
if (hash != other.hash)
|
|
|
|
return hash < other.hash;
|
2020-10-02 18:56:26 +02:00
|
|
|
if (thisAndNextLine != other.thisAndNextLine)
|
|
|
|
return thisAndNextLine;
|
2018-04-15 20:40:24 +02:00
|
|
|
return false;
|
2018-05-16 21:33:26 +02:00
|
|
|
}
|
2018-04-09 06:43:48 +02:00
|
|
|
|
2018-04-11 08:18:00 +02:00
|
|
|
/**
|
|
|
|
* Parse inline suppression in comment
|
|
|
|
* @param comment the full comment text
|
|
|
|
* @param errorMessage output parameter for error message (wrong suppression attribute)
|
|
|
|
* @return true if it is a inline comment.
|
|
|
|
*/
|
|
|
|
bool parseComment(std::string comment, std::string *errorMessage);
|
|
|
|
|
2018-04-09 06:43:48 +02:00
|
|
|
bool isSuppressed(const ErrorMessage &errmsg) const;
|
|
|
|
|
|
|
|
bool isMatch(const ErrorMessage &errmsg);
|
2020-11-04 21:01:48 +01:00
|
|
|
|
2018-04-09 06:43:48 +02:00
|
|
|
std::string getText() const;
|
|
|
|
|
2018-05-11 09:01:08 +02:00
|
|
|
bool isLocal() const {
|
|
|
|
return !fileName.empty() && fileName.find_first_of("?*") == std::string::npos;
|
|
|
|
}
|
|
|
|
|
2020-11-04 21:01:48 +01:00
|
|
|
bool isSameParameters(const Suppression &other) const {
|
|
|
|
return errorId == other.errorId &&
|
2020-11-06 19:50:05 +01:00
|
|
|
fileName == other.fileName &&
|
|
|
|
lineNumber == other.lineNumber &&
|
|
|
|
symbolName == other.symbolName &&
|
|
|
|
hash == other.hash &&
|
|
|
|
thisAndNextLine == other.thisAndNextLine;
|
2020-11-04 21:01:48 +01:00
|
|
|
}
|
|
|
|
|
2018-04-09 06:43:48 +02:00
|
|
|
std::string errorId;
|
|
|
|
std::string fileName;
|
|
|
|
int lineNumber;
|
|
|
|
std::string symbolName;
|
2020-07-21 11:27:03 +02:00
|
|
|
std::size_t hash;
|
2020-10-02 18:56:26 +02:00
|
|
|
bool thisAndNextLine; // Special case for backwards compatibility: { // cppcheck-suppress something
|
2018-04-09 06:43:48 +02:00
|
|
|
bool matched;
|
2022-03-30 19:24:53 +02:00
|
|
|
bool checked; // for inline suppressions, checked or not
|
2018-04-09 06:43:48 +02:00
|
|
|
|
2018-09-18 12:58:14 +02:00
|
|
|
enum { NO_LINE = -1 };
|
2011-08-22 18:54:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Don't show errors listed in the file.
|
|
|
|
* @param istr Open file stream where errors can be read.
|
|
|
|
* @return error message. empty upon success
|
|
|
|
*/
|
|
|
|
std::string parseFile(std::istream &istr);
|
|
|
|
|
2018-04-09 06:43:48 +02:00
|
|
|
/**
|
|
|
|
* @brief Don't show errors listed in the file.
|
|
|
|
* @param filename file name
|
|
|
|
* @return error message. empty upon success
|
|
|
|
*/
|
|
|
|
std::string parseXmlFile(const char *filename);
|
|
|
|
|
2020-02-23 18:04:24 +01:00
|
|
|
/**
|
|
|
|
* Parse multi inline suppression in comment
|
|
|
|
* @param comment the full comment text
|
|
|
|
* @param errorMessage output parameter for error message (wrong suppression attribute)
|
|
|
|
* @return empty vector if something wrong.
|
|
|
|
*/
|
2020-02-23 19:49:53 +01:00
|
|
|
static std::vector<Suppression> parseMultiSuppressComment(const std::string &comment, std::string *errorMessage);
|
2020-02-23 18:04:24 +01:00
|
|
|
|
2011-08-22 18:54:23 +02:00
|
|
|
/**
|
|
|
|
* @brief Don't show the given error.
|
|
|
|
* @param line Description of error to suppress (in id:file:line format).
|
|
|
|
* @return error message. empty upon success
|
|
|
|
*/
|
|
|
|
std::string addSuppressionLine(const std::string &line);
|
|
|
|
|
|
|
|
/**
|
2013-02-10 07:43:09 +01:00
|
|
|
* @brief Don't show this error. File and/or line are optional. In which case
|
2011-08-22 18:54:23 +02:00
|
|
|
* the errorId alone is used for filtering.
|
2018-06-07 08:33:32 +02:00
|
|
|
* @param suppression suppression details
|
2011-08-22 18:54:23 +02:00
|
|
|
* @return error message. empty upon success
|
|
|
|
*/
|
2018-04-09 06:43:48 +02:00
|
|
|
std::string addSuppression(const Suppression &suppression);
|
2011-08-22 18:54:23 +02:00
|
|
|
|
2020-11-04 21:01:48 +01:00
|
|
|
/**
|
|
|
|
* @brief Combine list of suppressions into the current suppressions.
|
|
|
|
* @param suppressions list of suppression details
|
|
|
|
* @return error message. empty upon success
|
|
|
|
*/
|
|
|
|
std::string addSuppressions(const std::list<Suppression> &suppressions);
|
|
|
|
|
2011-08-22 18:54:23 +02:00
|
|
|
/**
|
|
|
|
* @brief Returns true if this message should not be shown to the user.
|
2018-04-09 06:43:48 +02:00
|
|
|
* @param errmsg error message
|
2011-08-22 18:54:23 +02:00
|
|
|
* @return true if this error is suppressed.
|
|
|
|
*/
|
2018-04-09 06:43:48 +02:00
|
|
|
bool isSuppressed(const ErrorMessage &errmsg);
|
2011-08-22 18:54:23 +02:00
|
|
|
|
2023-03-03 18:37:09 +01:00
|
|
|
/**
|
|
|
|
* @brief Returns true if this message should not be shown to the user.
|
|
|
|
* @param errmsg error message
|
|
|
|
* @return true if this error is suppressed.
|
|
|
|
*/
|
|
|
|
bool isSuppressed(const ::ErrorMessage &errmsg);
|
|
|
|
|
2018-05-11 09:01:08 +02:00
|
|
|
/**
|
|
|
|
* @brief Returns true if this message should not be shown to the user, only uses local suppressions.
|
|
|
|
* @param errmsg error message
|
|
|
|
* @return true if this error is suppressed.
|
|
|
|
*/
|
|
|
|
bool isSuppressedLocal(const ErrorMessage &errmsg);
|
|
|
|
|
2018-04-24 22:19:24 +02:00
|
|
|
/**
|
|
|
|
* @brief Create an xml dump of suppressions
|
|
|
|
* @param out stream to write XML to
|
2021-08-07 20:51:18 +02:00
|
|
|
*/
|
2019-06-29 07:53:32 +02:00
|
|
|
void dump(std::ostream &out) const;
|
2018-04-24 22:19:24 +02:00
|
|
|
|
2011-08-22 18:54:23 +02:00
|
|
|
/**
|
|
|
|
* @brief Returns list of unmatched local (per-file) suppressions.
|
|
|
|
* @return list of unmatched suppressions
|
|
|
|
*/
|
2018-04-09 06:43:48 +02:00
|
|
|
std::list<Suppression> getUnmatchedLocalSuppressions(const std::string &file, const bool unusedFunctionChecking) const;
|
2011-08-22 18:54:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns list of unmatched global (glob pattern) suppressions.
|
|
|
|
* @return list of unmatched suppressions
|
|
|
|
*/
|
2018-04-09 06:43:48 +02:00
|
|
|
std::list<Suppression> getUnmatchedGlobalSuppressions(const bool unusedFunctionChecking) const;
|
|
|
|
|
2020-11-04 21:01:48 +01:00
|
|
|
/**
|
|
|
|
* @brief Returns list of all suppressions.
|
|
|
|
* @return list of suppressions
|
|
|
|
*/
|
2021-08-21 22:00:45 +02:00
|
|
|
const std::list<Suppression> &getSuppressions() const;
|
2020-11-04 21:01:48 +01:00
|
|
|
|
2022-03-30 19:24:53 +02:00
|
|
|
/**
|
|
|
|
* @brief Marks Inline Suppressions as checked if source line is in the token stream
|
|
|
|
*/
|
|
|
|
void markUnmatchedInlineSuppressionsAsChecked(const Tokenizer &tokenizer);
|
|
|
|
|
2018-04-09 06:43:48 +02:00
|
|
|
private:
|
|
|
|
/** @brief List of error which the user doesn't want to see. */
|
2018-06-17 08:16:37 +02:00
|
|
|
std::list<Suppression> mSuppressions;
|
2011-08-22 18:54:23 +02:00
|
|
|
};
|
|
|
|
|
2011-08-22 19:46:53 +02:00
|
|
|
/// @}
|
2013-09-04 20:59:49 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#endif // suppressionsH
|