cppcheck/lib/errorlogger.h

311 lines
9.8 KiB
C
Raw Normal View History

2009-02-19 18:57:27 +01:00
/*
* Cppcheck - A tool for static C/C++ code analysis
2022-02-05 11:45:17 +01:00
* Copyright (C) 2007-2022 Cppcheck team.
2009-02-19 18:57:27 +01: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/>.
2009-02-19 18:57:27 +01:00
*/
//---------------------------------------------------------------------------
2009-02-19 18:57:27 +01:00
#ifndef errorloggerH
#define errorloggerH
//---------------------------------------------------------------------------
2010-07-19 09:54:53 +02:00
#include "config.h"
2020-05-23 07:16:49 +02:00
#include "errortypes.h"
#include "suppressions.h"
2021-07-08 21:21:35 +02:00
#include "color.h"
#include <cstddef>
2017-05-16 14:07:23 +02:00
#include <fstream>
#include <list>
#include <string>
2017-05-16 14:07:23 +02:00
#include <vector>
2016-02-27 16:03:50 +01:00
/**
* CWE id (Common Weakness Enumeration)
* See https://cwe.mitre.org/ for further reference.
* */
2020-01-01 09:09:10 +01:00
// CWE list: https://cwe.mitre.org/data/published/cwe_v3.4.1.pdf
static const struct CWE CWE_USE_OF_UNINITIALIZED_VARIABLE(457U);
static const struct CWE CWE_NULL_POINTER_DEREFERENCE(476U);
static const struct CWE CWE_USE_OF_POTENTIALLY_DANGEROUS_FUNCTION(676U);
static const struct CWE CWE_INCORRECT_CALCULATION(682U);
static const struct CWE CWE_EXPIRED_POINTER_DEREFERENCE(825U);
2009-02-19 18:57:27 +01:00
class Token;
class TokenList;
2017-05-27 04:33:47 +02:00
2016-10-29 12:18:11 +02:00
namespace tinyxml2 {
class XMLElement;
}
2009-02-19 18:57:27 +01:00
/// @addtogroup Core
/// @{
2020-05-23 07:16:49 +02:00
/**
2021-08-07 20:51:18 +02:00
* Wrapper for error messages, provided by reportErr()
*/
2020-05-23 07:16:49 +02:00
class CPPCHECKLIB ErrorMessage {
public:
2011-03-06 13:29:12 +01:00
/**
2020-05-23 07:16:49 +02:00
* File name and line number.
* Internally paths are stored with / separator. When getting the filename
* it is by default converted to native separators.
2011-03-06 13:29:12 +01:00
*/
2020-05-23 07:16:49 +02:00
class CPPCHECKLIB FileLocation {
public:
FileLocation()
2021-08-07 20:51:18 +02:00
: fileIndex(0), line(0), column(0) {}
2020-05-23 07:16:49 +02:00
FileLocation(const std::string &file, int line, unsigned int column)
2021-08-07 20:51:18 +02:00
: fileIndex(0), line(line), column(column), mOrigFileName(file), mFileName(file) {}
2020-05-23 07:16:49 +02:00
FileLocation(const std::string &file, const std::string &info, int line, unsigned int column)
2021-08-07 20:51:18 +02:00
: fileIndex(0), line(line), column(column), mOrigFileName(file), mFileName(file), mInfo(info) {}
2020-05-23 07:16:49 +02:00
FileLocation(const Token* tok, const TokenList* tokenList);
FileLocation(const Token* tok, std::string info, const TokenList* tokenList);
2020-05-23 07:16:49 +02:00
2011-03-06 13:29:12 +01:00
/**
2020-05-23 07:16:49 +02:00
* Return the filename.
* @param convert If true convert path to native separators.
* @return filename.
2011-03-06 13:29:12 +01:00
*/
2020-05-23 07:16:49 +02:00
std::string getfile(bool convert = true) const;
2011-03-06 13:29:12 +01:00
/**
2020-05-23 07:16:49 +02:00
* Filename with the whole path (no --rp)
* @param convert If true convert path to native separators.
* @return filename.
2011-03-06 13:29:12 +01:00
*/
2020-05-23 07:16:49 +02:00
std::string getOrigFile(bool convert = true) const;
2011-03-06 13:29:12 +01:00
/**
2020-05-23 07:16:49 +02:00
* Set the filename.
* @param file Filename to set.
2011-03-06 13:29:12 +01:00
*/
2020-05-23 07:16:49 +02:00
void setfile(const std::string &file);
2011-03-06 13:29:12 +01:00
/**
2020-05-23 07:16:49 +02:00
* @return the location as a string. Format: [file:line]
2011-03-06 13:29:12 +01:00
*/
2020-05-23 07:16:49 +02:00
std::string stringify() const;
2011-03-06 13:29:12 +01:00
2020-05-23 07:16:49 +02:00
unsigned int fileIndex;
int line; // negative value means "no line"
unsigned int column;
2020-05-23 07:16:49 +02:00
std::string getinfo() const {
return mInfo;
}
void setinfo(const std::string &i) {
mInfo = i;
}
2020-05-23 07:16:49 +02:00
private:
std::string mOrigFileName;
std::string mFileName;
std::string mInfo;
};
ErrorMessage(std::list<FileLocation> callStack,
std::string file1,
2020-05-23 07:16:49 +02:00
Severity::SeverityType severity,
const std::string &msg,
std::string id, Certainty::CertaintyLevel certainty);
ErrorMessage(std::list<FileLocation> callStack,
std::string file1,
2020-05-23 07:16:49 +02:00
Severity::SeverityType severity,
const std::string &msg,
std::string id,
2020-05-23 07:16:49 +02:00
const CWE &cwe,
Certainty::CertaintyLevel certainty);
2020-05-23 07:16:49 +02:00
ErrorMessage(const std::list<const Token*>& callstack,
const TokenList* list,
Severity::SeverityType severity,
std::string id,
2020-05-23 07:16:49 +02:00
const std::string& msg,
Certainty::CertaintyLevel certainty);
2020-05-23 07:16:49 +02:00
ErrorMessage(const std::list<const Token*>& callstack,
const TokenList* list,
Severity::SeverityType severity,
std::string id,
2020-05-23 07:16:49 +02:00
const std::string& msg,
const CWE &cwe,
Certainty::CertaintyLevel certainty);
2020-05-23 07:16:49 +02:00
ErrorMessage(const ErrorPath &errorPath,
const TokenList *tokenList,
Severity::SeverityType severity,
const char id[],
const std::string &msg,
const CWE &cwe,
Certainty::CertaintyLevel certainty);
2020-05-23 07:16:49 +02:00
ErrorMessage();
explicit ErrorMessage(const tinyxml2::XMLElement * const errmsg);
2009-02-19 18:57:27 +01:00
/**
2020-05-23 07:16:49 +02:00
* Format the error message in XML format
2009-02-19 18:57:27 +01:00
*/
2020-05-23 07:16:49 +02:00
std::string toXML() const;
static std::string getXMLHeader(const std::string& productName);
2020-05-23 07:16:49 +02:00
static std::string getXMLFooter();
2020-05-23 07:16:49 +02:00
/**
* Format the error message into a string.
* @param verbose use verbose message
* @param templateFormat Empty string to use default output format
* or template to be used. E.g. "{file}:{line},{severity},{id},{message}"
* @param templateLocation Format Empty string to use default output format
* or template to be used. E.g. "{file}:{line},{info}"
2021-08-07 20:51:18 +02:00
* @return formatted string
2020-05-23 07:16:49 +02:00
*/
std::string toString(bool verbose,
const std::string &templateFormat = emptyString,
const std::string &templateLocation = emptyString) const;
2020-05-23 07:16:49 +02:00
std::string serialize() const;
bool deserialize(const std::string &data);
2020-05-23 07:16:49 +02:00
std::list<FileLocation> callStack;
std::string id;
2020-05-23 07:16:49 +02:00
/** For GUI rechecking; source file (not header) */
std::string file0;
2020-05-23 07:16:49 +02:00
Severity::SeverityType severity;
CWE cwe;
Certainty::CertaintyLevel certainty;
2020-07-21 11:27:03 +02:00
/** Warning hash */
std::size_t hash;
2020-05-23 07:16:49 +02:00
/** set short and verbose messages */
void setmsg(const std::string &msg);
2020-05-23 07:16:49 +02:00
/** Short message (single line short message) */
const std::string &shortMessage() const {
return mShortMessage;
}
2020-05-23 07:16:49 +02:00
/** Verbose message (may be the same as the short message) */
const std::string &verboseMessage() const {
return mVerboseMessage;
}
2020-05-23 07:16:49 +02:00
/** Symbol names */
const std::string &symbolNames() const {
return mSymbolNames;
}
2020-05-23 07:16:49 +02:00
Suppressions::ErrorMessage toSuppressionsErrorMessage() const;
2020-05-23 07:16:49 +02:00
private:
static std::string fixInvalidChars(const std::string& raw);
2020-05-23 07:16:49 +02:00
/** Short message */
std::string mShortMessage;
2020-05-23 07:16:49 +02:00
/** Verbose message */
std::string mVerboseMessage;
2020-05-23 07:16:49 +02:00
/** symbol names */
std::string mSymbolNames;
};
2009-02-19 18:57:27 +01:00
2020-05-23 07:16:49 +02:00
/**
* @brief This is an interface, which the class responsible of error logging
* should implement.
*/
class CPPCHECKLIB ErrorLogger {
protected:
std::ofstream plistFile;
public:
2021-08-07 20:51:18 +02:00
ErrorLogger() {}
2017-05-16 14:07:23 +02:00
virtual ~ErrorLogger() {
if (plistFile.is_open()) {
plistFile << ErrorLogger::plistFooter();
plistFile.close();
}
}
2009-02-19 18:57:27 +01:00
/**
* Information about progress is directed here.
* Override this to receive the progress messages.
*
* @param outmsg Message to show e.g. "Checking main.cpp..."
2009-02-19 18:57:27 +01:00
*/
2021-07-08 21:21:35 +02:00
virtual void reportOut(const std::string &outmsg, Color c = Color::Reset) = 0;
2009-02-19 18:57:27 +01:00
/**
* Information about found errors and warnings is directed
* here. Override this to receive the errormessages.
*
* @param msg Location and other information about the found error.
2009-02-19 18:57:27 +01:00
*/
2020-05-23 07:16:49 +02:00
virtual void reportErr(const ErrorMessage &msg) = 0;
2009-02-19 18:57:27 +01:00
/**
* Report progress to client
* @param filename main file that is checked
* @param stage for example preprocess / tokenize / simplify / check
* @param value progress value (0-100)
*/
2014-11-20 14:20:09 +01:00
virtual void reportProgress(const std::string &filename, const char stage[], const std::size_t value) {
(void)filename;
(void)stage;
(void)value;
}
/**
* Output information messages.
* @param msg Location and other information about the found error.
*/
2020-05-23 07:16:49 +02:00
virtual void reportInfo(const ErrorMessage &msg) {
2012-06-21 19:05:14 +02:00
reportErr(msg);
}
/**
* Report unmatched suppressions
* @param unmatched list of unmatched suppressions (from Settings::Suppressions::getUnmatched(Local|Global)Suppressions)
* @return true is returned if errors are reported
*/
bool reportUnmatchedSuppressions(const std::list<Suppressions::Suppression> &unmatched);
2020-05-23 07:16:49 +02:00
static std::string callStackToString(const std::list<ErrorMessage::FileLocation> &callStack);
/**
* Convert XML-sensitive characters into XML entities
* @param str The input string containing XML-sensitive characters
* @return The output string containing XML entities
*/
static std::string toxml(const std::string &str);
2017-05-16 14:07:23 +02:00
static std::string plistHeader(const std::string &version, const std::vector<std::string> &files);
2020-05-23 07:16:49 +02:00
static std::string plistData(const ErrorMessage &msg);
2017-05-16 14:07:23 +02:00
static const char *plistFooter() {
return " </array>\r\n"
"</dict>\r\n"
"</plist>";
}
2009-02-19 18:57:27 +01:00
};
/** Replace substring. Example replaceStr("1,NR,3", "NR", "2") => "1,2,3" */
std::string replaceStr(std::string s, const std::string &from, const std::string &to);
/// @}
//---------------------------------------------------------------------------
#endif // errorloggerH