2009-02-19 18:57:27 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 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
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-02-19 18:57:27 +01:00
|
|
|
*/
|
|
|
|
|
2013-09-04 20:59:49 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2009-02-19 18:57:27 +01:00
|
|
|
#ifndef errorloggerH
|
|
|
|
#define errorloggerH
|
2013-09-04 20:59:49 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2010-07-19 09:54:53 +02:00
|
|
|
|
2012-06-10 14:19:09 +02:00
|
|
|
#include "config.h"
|
2020-05-23 07:16:49 +02:00
|
|
|
#include "errortypes.h"
|
2021-07-08 21:21:35 +02:00
|
|
|
#include "color.h"
|
2011-02-16 02:12:15 +01:00
|
|
|
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <cstddef>
|
2015-11-29 10:49:10 +01:00
|
|
|
#include <list>
|
2023-08-29 12:00:52 +02:00
|
|
|
#include <set>
|
2015-11-29 10:49:10 +01:00
|
|
|
#include <string>
|
2022-09-16 18:59:15 +02:00
|
|
|
#include <utility>
|
2017-05-16 14:07:23 +02:00
|
|
|
#include <vector>
|
2015-11-29 10:49:10 +01:00
|
|
|
|
2009-02-19 18:57:27 +01:00
|
|
|
class Token;
|
2012-05-06 10:17:15 +02:00
|
|
|
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
|
|
|
|
2009-07-17 10:49:01 +02: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 {
|
2010-07-14 16:30:03 +02:00
|
|
|
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
|
|
|
|
2022-09-29 21:47:17 +02:00
|
|
|
explicit FileLocation(const std::string &file, int line = 0, unsigned int column = 0)
|
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
|
|
|
|
2022-09-16 18:59:15 +02:00
|
|
|
FileLocation(const std::string &file, std::string info, int line, unsigned int column)
|
|
|
|
: fileIndex(0), line(line), column(column), mOrigFileName(file), mFileName(file), mInfo(std::move(info)) {}
|
2020-05-23 07:16:49 +02:00
|
|
|
|
|
|
|
FileLocation(const Token* tok, const TokenList* tokenList);
|
2022-07-28 22:51:45 +02:00
|
|
|
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
|
|
|
*/
|
2022-12-09 19:34:51 +01:00
|
|
|
void setfile(std::string file);
|
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 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;
|
2010-07-14 16:30:03 +02:00
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
std::string getinfo() const {
|
|
|
|
return mInfo;
|
|
|
|
}
|
|
|
|
void setinfo(const std::string &i) {
|
|
|
|
mInfo = i;
|
|
|
|
}
|
2017-05-16 22:38:13 +02:00
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
private:
|
|
|
|
std::string mOrigFileName;
|
|
|
|
std::string mFileName;
|
|
|
|
std::string mInfo;
|
|
|
|
};
|
2017-05-16 22:38:13 +02:00
|
|
|
|
2022-07-28 22:51:45 +02:00
|
|
|
ErrorMessage(std::list<FileLocation> callStack,
|
|
|
|
std::string file1,
|
2020-05-23 07:16:49 +02:00
|
|
|
Severity::SeverityType severity,
|
|
|
|
const std::string &msg,
|
2023-01-08 19:31:54 +01:00
|
|
|
std::string id, Certainty certainty);
|
2022-07-28 22:51:45 +02:00
|
|
|
ErrorMessage(std::list<FileLocation> callStack,
|
|
|
|
std::string file1,
|
2020-05-23 07:16:49 +02:00
|
|
|
Severity::SeverityType severity,
|
|
|
|
const std::string &msg,
|
2022-07-28 22:51:45 +02:00
|
|
|
std::string id,
|
2020-05-23 07:16:49 +02:00
|
|
|
const CWE &cwe,
|
2023-01-08 19:31:54 +01:00
|
|
|
Certainty certainty);
|
2020-05-23 07:16:49 +02:00
|
|
|
ErrorMessage(const std::list<const Token*>& callstack,
|
|
|
|
const TokenList* list,
|
|
|
|
Severity::SeverityType severity,
|
2022-07-28 22:51:45 +02:00
|
|
|
std::string id,
|
2020-05-23 07:16:49 +02:00
|
|
|
const std::string& msg,
|
2023-01-08 19:31:54 +01:00
|
|
|
Certainty certainty);
|
2020-05-23 07:16:49 +02:00
|
|
|
ErrorMessage(const std::list<const Token*>& callstack,
|
|
|
|
const TokenList* list,
|
|
|
|
Severity::SeverityType severity,
|
2022-07-28 22:51:45 +02:00
|
|
|
std::string id,
|
2020-05-23 07:16:49 +02:00
|
|
|
const std::string& msg,
|
|
|
|
const CWE &cwe,
|
2023-01-08 19:31:54 +01:00
|
|
|
Certainty 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,
|
2023-01-08 19:31:54 +01:00
|
|
|
Certainty 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;
|
2010-11-11 19:54:43 +01:00
|
|
|
|
2023-07-02 18:13:11 +02:00
|
|
|
static std::string getXMLHeader(std::string productName);
|
2020-05-23 07:16:49 +02:00
|
|
|
static std::string getXMLFooter();
|
2009-06-15 20:36:39 +02:00
|
|
|
|
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;
|
2010-11-11 19:54:43 +01:00
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
std::string serialize() const;
|
2022-12-07 18:00:45 +01:00
|
|
|
void deserialize(const std::string &data);
|
2010-11-11 19:54:43 +01:00
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
std::list<FileLocation> callStack;
|
|
|
|
std::string id;
|
2010-11-11 19:54:43 +01:00
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
/** For GUI rechecking; source file (not header) */
|
|
|
|
std::string file0;
|
2009-09-05 21:01:49 +02:00
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
Severity::SeverityType severity;
|
|
|
|
CWE cwe;
|
2023-01-08 19:31:54 +01:00
|
|
|
Certainty certainty;
|
2012-05-14 20:46:23 +02:00
|
|
|
|
2020-07-21 11:27:03 +02:00
|
|
|
/** Warning hash */
|
|
|
|
std::size_t hash;
|
2020-07-14 22:30:42 +02:00
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
/** set short and verbose messages */
|
|
|
|
void setmsg(const std::string &msg);
|
2010-11-11 19:54:43 +01:00
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
/** Short message (single line short message) */
|
|
|
|
const std::string &shortMessage() const {
|
|
|
|
return mShortMessage;
|
|
|
|
}
|
2010-11-11 19:54:43 +01:00
|
|
|
|
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;
|
|
|
|
}
|
2010-11-11 19:54:43 +01:00
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
/** Symbol names */
|
|
|
|
const std::string &symbolNames() const {
|
|
|
|
return mSymbolNames;
|
|
|
|
}
|
2018-04-09 06:43:48 +02:00
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
private:
|
|
|
|
static std::string fixInvalidChars(const std::string& raw);
|
2015-06-18 19:07:51 +02:00
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
/** Short message */
|
|
|
|
std::string mShortMessage;
|
2010-11-11 19:54:43 +01:00
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
/** Verbose message */
|
|
|
|
std::string mVerboseMessage;
|
2018-04-09 06:43:48 +02:00
|
|
|
|
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 {
|
|
|
|
public:
|
2023-08-16 17:13:36 +02:00
|
|
|
ErrorLogger() = default;
|
|
|
|
virtual ~ErrorLogger() = default;
|
2009-02-19 18:57:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Information about progress is directed here.
|
|
|
|
* Override this to receive the progress messages.
|
|
|
|
*
|
2009-06-20 11:54:49 +02:00
|
|
|
* @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.
|
|
|
|
*
|
2012-06-18 23:15:48 +02:00
|
|
|
* @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
|
|
|
|
2010-07-24 18:58:52 +02:00
|
|
|
/**
|
2010-08-03 16:36:21 +02: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)
|
2010-07-24 18:58:52 +02:00
|
|
|
*/
|
2014-11-20 14:20:09 +01:00
|
|
|
virtual void reportProgress(const std::string &filename, const char stage[], const std::size_t value) {
|
2010-08-03 16:36:21 +02:00
|
|
|
(void)filename;
|
|
|
|
(void)stage;
|
|
|
|
(void)value;
|
|
|
|
}
|
2010-07-24 18:58:52 +02:00
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
static std::string callStackToString(const std::list<ErrorMessage::FileLocation> &callStack);
|
2015-12-07 18:11:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert XML-sensitive characters into XML entities
|
|
|
|
* @param str The input string containing XML-sensitive characters
|
2016-11-04 15:01:05 +01:00
|
|
|
* @return The output string containing XML entities
|
2015-12-07 18:11:13 +01:00
|
|
|
*/
|
|
|
|
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>";
|
|
|
|
}
|
2023-08-29 12:00:52 +02:00
|
|
|
|
|
|
|
static bool isCriticalErrorId(const std::string& id) {
|
|
|
|
return mCriticalErrorIds.count(id) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const std::set<std::string> mCriticalErrorIds;
|
2009-02-19 18:57:27 +01:00
|
|
|
};
|
2009-07-13 10:16:31 +02:00
|
|
|
|
2018-12-28 13:11:54 +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);
|
|
|
|
|
2023-04-08 22:19:52 +02:00
|
|
|
/** replaces the static parts of the location template **/
|
|
|
|
CPPCHECKLIB void substituteTemplateFormatStatic(std::string& templateFormat);
|
|
|
|
|
|
|
|
/** replaces the static parts of the location template **/
|
|
|
|
CPPCHECKLIB void substituteTemplateLocationStatic(std::string& templateLocation);
|
|
|
|
|
2009-07-17 10:49:01 +02:00
|
|
|
/// @}
|
2013-09-04 20:59:49 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#endif // errorloggerH
|