2009-02-19 18:57:27 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2020-05-10 11:16:32 +02:00
|
|
|
* Copyright (C) 2007-2020 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"
|
2011-08-22 18:54:23 +02:00
|
|
|
#include "suppressions.h"
|
2011-02-16 02:12:15 +01:00
|
|
|
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <cstddef>
|
2017-05-16 14:07:23 +02:00
|
|
|
#include <fstream>
|
2015-11-29 10:49:10 +01:00
|
|
|
#include <list>
|
|
|
|
#include <string>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <utility>
|
2017-05-16 14:07:23 +02:00
|
|
|
#include <vector>
|
2015-11-29 10:49:10 +01:00
|
|
|
|
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);
|
Mapped toomanyconfigs ,AssignmentAddressToInteger
,AssignmentIntegerToAddress ,CastIntegerToAddressAtReturn
,CastAddressToIntegerAtReturn ,assertWithSideEffect ,assignmentInAssert
,uselessAssignmentArg ,uselessAssignmentPtrArg
,comparisonOfFuncReturningBoolError
,comparisonOfTwoFuncsReturningBoolError ,comparisonOfBoolWithBoolError
,incrementboolean ,comparisonOfBoolWithInt ,compareBoolExpressionWithInt
,negativeIndex ,pointerOutOfBounds ,arrayIndexThenCheck
,possibleBufferAccessOutOfBounds ,argumentSize
,arrayIndexOutOfBoundsCond ,noConstructor ,copyCtorPointerCopying
,noCopyConstructor ,uninitMemberVar ,operatorEqVarError
,unusedPrivateFunction ,memsetClassFloat ,mallocOnClassWarning
,operatorEq ,thisSubtraction ,operatorEqRetRefThis ,operatorEqToSelf
,useInitializationList ,duplInheritedMember ,assignIfError
,comparisonError ,multiCondition ,mismatchingBitAnd
,oppositeInnerCondition ,incorrectLogicOperator ,redundantCondition
,moduloAlwaysTrueFalse to their CWEs ids.
2016-02-20 23:56:36 +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
|
|
|
/**
|
|
|
|
* Wrapper for error messages, provided by reportErr()
|
|
|
|
*/
|
|
|
|
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()
|
|
|
|
: 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) {
|
|
|
|
}
|
|
|
|
|
|
|
|
FileLocation(const Token* tok, const TokenList* tokenList);
|
|
|
|
FileLocation(const Token* tok, const std::string &info, const TokenList* tokenList);
|
|
|
|
|
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;
|
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
|
|
|
|
2020-05-23 07:16:49 +02: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();
|
|
|
|
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
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
static std::string getXMLHeader();
|
|
|
|
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}"
|
|
|
|
* @return formatted string
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
bool 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;
|
|
|
|
/** For GUI bug hunting; function name */
|
|
|
|
std::string function;
|
|
|
|
/** For GUI bug hunting; incomplete analysis */
|
|
|
|
bool incomplete;
|
2009-09-05 21:01:49 +02:00
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
Severity::SeverityType severity;
|
|
|
|
CWE cwe;
|
|
|
|
bool inconclusive;
|
2012-05-14 20:46:23 +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
|
|
|
Suppressions::ErrorMessage toSuppressionsErrorMessage() const;
|
2018-04-09 06:43:48 +02:00
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* 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);
|
2010-08-30 17:44:46 +02:00
|
|
|
|
2020-05-23 07:16:49 +02:00
|
|
|
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 {
|
|
|
|
protected:
|
|
|
|
std::ofstream plistFile;
|
|
|
|
public:
|
2010-08-03 16:36:21 +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.
|
|
|
|
*
|
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
|
|
|
*/
|
|
|
|
virtual void reportOut(const std::string &outmsg) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2012-06-18 23:15:48 +02:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2012-06-18 23:15:48 +02:00
|
|
|
|
2020-01-18 07:25:39 +01:00
|
|
|
virtual void bughuntingReport(const std::string &str) = 0;
|
2019-12-27 19:05:10 +01:00
|
|
|
|
2011-02-16 02:12:15 +01:00
|
|
|
/**
|
2019-01-21 20:33:22 +01:00
|
|
|
* Report unmatched suppressions
|
2011-02-16 02:12:15 +01:00
|
|
|
* @param unmatched list of unmatched suppressions (from Settings::Suppressions::getUnmatched(Local|Global)Suppressions)
|
2019-01-21 20:33:22 +01:00
|
|
|
* @return true is returned if errors are reported
|
2011-02-16 02:12:15 +01:00
|
|
|
*/
|
2019-01-21 20:33:22 +01:00
|
|
|
bool reportUnmatchedSuppressions(const std::list<Suppressions::Suppression> &unmatched);
|
2011-02-16 02:12:15 +01: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>";
|
|
|
|
}
|
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);
|
|
|
|
|
2009-07-17 10:49:01 +02:00
|
|
|
/// @}
|
2013-09-04 20:59:49 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#endif // errorloggerH
|