cppcheck/lib/errorlogger.h

290 lines
9.1 KiB
C
Raw Normal View History

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
* 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"
2021-07-08 21:21:35 +02:00
#include "color.h"
#include <cstddef>
#include <list>
#include <set>
#include <string>
#include <utility>
2017-05-16 14:07:23 +02:00
#include <vector>
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
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
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);
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
*/
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;
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 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 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 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 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 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(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;
void 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 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;
}
static ErrorMessage fromInternalError(const InternalError &internalError, const TokenList *tokenList, const std::string &filename, const std::string& msg = emptyString);
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 {
public:
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.
*
* @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;
}
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>";
}
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
};
/** 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);
/** 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);
/// @}
//---------------------------------------------------------------------------
#endif // errorloggerH