cppcheck/lib/errorlogger.h

447 lines
14 KiB
C
Raw Normal View History

2009-02-19 18:57:27 +01:00
/*
* Cppcheck - A tool for static C/C++ code analysis
2019-02-09 07:24:06 +01:00
* Copyright (C) 2007-2019 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"
#include "suppressions.h"
2017-05-27 04:33:47 +02:00
#include <cstddef>
2017-05-16 14:07:23 +02:00
#include <fstream>
#include <list>
#include <string>
2017-05-27 04:33:47 +02:00
#include <utility>
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.
* */
struct CWE {
explicit CWE(unsigned short cweId) : id(cweId) {}
2016-02-27 16:03:50 +01:00
unsigned short id;
};
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
/// @{
/** @brief Simple container to be thrown when internal error is detected. */
struct InternalError {
enum Type {AST, SYNTAX, UNKNOWN_MACRO, INTERNAL, LIMIT, INSTANTIATION};
InternalError(const Token *tok, const std::string &errorMsg, Type type = INTERNAL);
const Token *token;
std::string errorMessage;
Type type;
std::string id;
};
/** @brief enum class for severity. Used when reporting errors. */
class CPPCHECKLIB Severity {
public:
2011-03-06 13:29:12 +01:00
/**
* Message severities.
*/
2011-10-13 20:53:06 +02:00
enum SeverityType {
2011-03-06 13:29:12 +01:00
/**
* No severity (default value).
*/
none,
/**
* Programming error.
* This indicates severe error like memory leak etc.
* The error is certain.
2011-03-06 13:29:12 +01:00
*/
error,
/**
* Warning.
* Used for dangerous coding style that can cause severe runtime errors.
* For example: forgetting to initialize a member variable in a constructor.
2011-03-06 13:29:12 +01:00
*/
warning,
/**
* Style warning.
* Used for general code cleanup recommendations. Fixing these
* will not fix any bugs but will make the code easier to maintain.
* For example: redundant code, unreachable code, etc.
2011-03-06 13:29:12 +01:00
*/
style,
/**
* Performance warning.
* Not an error as is but suboptimal code and fixing it probably leads
* to faster performance of the compiled code.
*/
performance,
/**
* Portability warning.
* This warning indicates the code is not properly portable for
* different platforms and bitnesses (32/64 bit). If the code is meant
* to compile in different platforms and bitnesses these warnings
* should be fixed.
*/
portability,
/**
* Checking information.
* Information message about the checking (process) itself. These
* messages inform about header files not found etc issues that are
* not errors in the code but something user needs to know.
*/
information,
/**
* Debug message.
* Debug-mode message useful for the developers.
*/
debug
2011-03-06 13:29:12 +01:00
};
2014-11-20 14:20:09 +01:00
static std::string toString(SeverityType severity) {
2011-10-13 20:53:06 +02:00
switch (severity) {
case none:
return "";
case error:
return "error";
case warning:
return "warning";
case style:
return "style";
case performance:
return "performance";
case portability:
return "portability";
2010-12-22 19:53:17 +01:00
case information:
return "information";
case debug:
return "debug";
}
throw InternalError(nullptr, "Unknown severity");
}
2014-11-20 14:20:09 +01:00
static SeverityType fromString(const std::string &severity) {
if (severity.empty())
return none;
if (severity == "none")
return none;
if (severity == "error")
return error;
if (severity == "warning")
return warning;
if (severity == "style")
return style;
if (severity == "performance")
return performance;
if (severity == "portability")
return portability;
2010-12-22 19:53:17 +01:00
if (severity == "information")
return information;
if (severity == "debug")
return debug;
return none;
}
};
typedef std::pair<const Token *, std::string> ErrorPathItem;
typedef std::list<ErrorPathItem> ErrorPath;
2009-02-19 18:57:27 +01:00
/**
* @brief This is an interface, which the class responsible of error logging
2009-02-19 18:57:27 +01:00
* should implement.
*/
class CPPCHECKLIB ErrorLogger {
2017-05-16 14:07:23 +02:00
protected:
std::ofstream plistFile;
2009-02-19 18:57:27 +01:00
public:
/**
* Wrapper for error messages, provided by reportErr()
*/
class CPPCHECKLIB ErrorMessage {
2009-02-19 18:57:27 +01:00
public:
/**
* File name and line number.
* Internally paths are stored with / separator. When getting the filename
* it is by default converted to native separators.
2009-02-19 18:57:27 +01:00
*/
class CPPCHECKLIB FileLocation {
2009-02-19 18:57:27 +01:00
public:
FileLocation()
: fileIndex(0), line(0), column(0) {
}
FileLocation(const std::string &file, int line, int column)
: fileIndex(0), line(line), column(column), mOrigFileName(file), mFileName(file) {
}
FileLocation(const std::string &file, const std::string &info, int line, int column)
: fileIndex(0), line(line), column(column), mOrigFileName(file), mFileName(file), mInfo(info) {
}
2017-05-17 15:22:51 +02:00
FileLocation(const Token* tok, const TokenList* tokenList);
FileLocation(const Token* tok, const std::string &info, const TokenList* tokenList);
/**
* Return the filename.
* @param convert If true convert path to native separators.
* @return filename.
*/
std::string getfile(bool convert = true) const;
/**
* Filename with the whole path (no --rp)
* @param convert If true convert path to native separators.
* @return filename.
*/
std::string getOrigFile(bool convert = true) const;
/**
* Set the filename.
* @param file Filename to set.
*/
void setfile(const std::string &file);
/**
2014-04-27 21:42:10 +02:00
* @return the location as a string. Format: [file:line]
*/
std::string stringify() const;
2017-05-17 15:22:51 +02:00
unsigned int fileIndex;
int line; // negative value means "no line"
unsigned int column;
std::string getinfo() const {
2018-06-17 07:52:59 +02:00
return mInfo;
}
void setinfo(const std::string &i) {
2018-06-17 07:52:59 +02:00
mInfo = i;
}
private:
std::string mOrigFileName;
2018-06-17 07:52:59 +02:00
std::string mFileName;
std::string mInfo;
2009-02-19 18:57:27 +01:00
};
ErrorMessage(const std::list<FileLocation> &callStack,
const std::string& file1,
Severity::SeverityType severity,
const std::string &msg,
const std::string &id, bool inconclusive);
ErrorMessage(const std::list<FileLocation> &callStack,
const std::string& file1,
Severity::SeverityType severity,
const std::string &msg,
const std::string &id,
const CWE &cwe,
bool inconclusive);
ErrorMessage(const std::list<const Token*>& callstack,
const TokenList* list,
Severity::SeverityType severity,
const std::string& id,
const std::string& msg,
bool inconclusive);
ErrorMessage(const std::list<const Token*>& callstack,
const TokenList* list,
Severity::SeverityType severity,
const std::string& id,
const std::string& msg,
const CWE &cwe,
bool inconclusive);
ErrorMessage(const ErrorPath &errorPath,
const TokenList *tokenList,
Severity::SeverityType severity,
const char id[],
const std::string &msg,
const CWE &cwe,
bool inconclusive);
ErrorMessage();
2016-10-29 12:35:14 +02:00
explicit ErrorMessage(const tinyxml2::XMLElement * const errmsg);
/**
* Format the error message in XML format
*/
2017-07-29 18:56:22 +02:00
std::string toXML() const;
2017-07-29 18:56:22 +02:00
static std::string getXMLHeader();
static std::string getXMLFooter();
/**
* 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}"
2014-04-27 21:42:10 +02:00
* @return formatted string
*/
std::string toString(bool verbose,
const std::string &templateFormat = emptyString,
const std::string &templateLocation = emptyString) const;
std::string serialize() const;
bool deserialize(const std::string &data);
std::list<FileLocation> callStack;
std::string id;
/** For GUI rechecking; source file (not header) */
std::string file0;
/** For GUI bug hunting; function name */
std::string function;
/** For GUI bug hunting; incomplete analysis */
bool incomplete;
Severity::SeverityType severity;
CWE cwe;
bool inconclusive;
/** set short and verbose messages */
void setmsg(const std::string &msg);
/** Short message (single line short message) */
2014-11-20 14:20:09 +01:00
const std::string &shortMessage() const {
2018-06-17 07:54:50 +02:00
return mShortMessage;
}
/** Verbose message (may be the same as the short message) */
2014-11-20 14:20:09 +01:00
const std::string &verboseMessage() const {
2018-06-17 07:55:47 +02:00
return mVerboseMessage;
}
/** Symbol names */
const std::string &symbolNames() const {
2018-06-17 07:59:48 +02:00
return mSymbolNames;
}
Suppressions::ErrorMessage toSuppressionsErrorMessage() const;
private:
/**
2010-12-15 18:45:53 +01:00
* Replace all occurrences of searchFor with replaceWith in the
* given source.
* @param source The string to modify
* @param searchFor What should be searched for
* @param replaceWith What will replace the found item
*/
static void findAndReplace(std::string &source, const std::string &searchFor, const std::string &replaceWith);
static std::string fixInvalidChars(const std::string& raw);
/** Short message */
2018-06-17 07:54:50 +02:00
std::string mShortMessage;
/** Verbose message */
2018-06-17 07:55:47 +02:00
std::string mVerboseMessage;
/** symbol names */
2018-06-17 07:59:48 +02:00
std::string mSymbolNames;
2009-02-19 18:57:27 +01: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
*/
virtual void reportOut(const std::string &outmsg) = 0;
/**
* 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
*/
virtual void reportErr(const ErrorLogger::ErrorMessage &msg) = 0;
/**
* 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.
*/
2014-11-20 14:20:09 +01:00
virtual void reportInfo(const ErrorLogger::ErrorMessage &msg) {
2012-06-21 19:05:14 +02:00
reportErr(msg);
}
2020-01-18 07:25:39 +01:00
virtual void bughuntingReport(const std::string &str) = 0;
2019-12-27 19:05:10 +01:00
/**
* 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);
static std::string callStackToString(const std::list<ErrorLogger::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);
static std::string plistData(const ErrorLogger::ErrorMessage &msg);
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