2009-02-19 18:57:27 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2010-04-13 21:23:17 +02:00
|
|
|
* Copyright (C) 2007-2010 Daniel Marjamäki and 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
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef errorloggerH
|
|
|
|
#define errorloggerH
|
2010-07-19 09:54:53 +02:00
|
|
|
|
2009-02-19 18:57:27 +01:00
|
|
|
#include <list>
|
|
|
|
#include <string>
|
2010-07-26 22:05:17 +02:00
|
|
|
|
2009-02-19 18:57:27 +01:00
|
|
|
class Token;
|
|
|
|
class Tokenizer;
|
|
|
|
|
2009-07-17 10:49:01 +02:00
|
|
|
/// @addtogroup Core
|
|
|
|
/// @{
|
|
|
|
|
2010-07-14 16:30:03 +02:00
|
|
|
/** @brief enum class for severity. Used when reporting errors. */
|
|
|
|
class Severity
|
|
|
|
{
|
|
|
|
public:
|
2010-12-11 21:54:06 +01:00
|
|
|
enum SeverityType { none, error, warning, style, performance, portability, debug };
|
2010-07-14 16:40:18 +02:00
|
|
|
static std::string toString(SeverityType severity)
|
2010-07-14 16:30:03 +02:00
|
|
|
{
|
|
|
|
switch (severity)
|
|
|
|
{
|
2010-07-14 19:21:39 +02:00
|
|
|
case none:
|
|
|
|
return "";
|
2010-07-14 16:30:03 +02:00
|
|
|
case error:
|
|
|
|
return "error";
|
2010-10-17 14:41:00 +02:00
|
|
|
case warning:
|
|
|
|
return "warning";
|
2010-07-14 16:30:03 +02:00
|
|
|
case style:
|
|
|
|
return "style";
|
2010-10-17 14:41:00 +02:00
|
|
|
case performance:
|
|
|
|
return "performance";
|
2010-12-11 21:54:06 +01:00
|
|
|
case portability:
|
|
|
|
return "portability";
|
2010-07-14 18:41:29 +02:00
|
|
|
case debug:
|
|
|
|
return "debug";
|
2010-07-14 16:30:03 +02:00
|
|
|
};
|
|
|
|
return "???";
|
|
|
|
}
|
2010-07-14 18:36:34 +02:00
|
|
|
static SeverityType fromString(const std::string &severity)
|
|
|
|
{
|
2010-07-14 19:21:39 +02:00
|
|
|
if (severity.empty())
|
|
|
|
return none;
|
|
|
|
if (severity == "none")
|
|
|
|
return none;
|
2010-07-14 18:36:34 +02:00
|
|
|
if (severity == "error")
|
|
|
|
return error;
|
2010-10-17 14:41:00 +02:00
|
|
|
if (severity == "warning")
|
|
|
|
return warning;
|
2010-07-14 18:36:34 +02:00
|
|
|
if (severity == "style")
|
|
|
|
return style;
|
2010-10-17 14:41:00 +02:00
|
|
|
if (severity == "performance")
|
|
|
|
return performance;
|
2010-12-11 21:54:06 +01:00
|
|
|
if (severity == "portability")
|
|
|
|
return portability;
|
2010-07-14 18:41:29 +02:00
|
|
|
if (severity == "debug")
|
|
|
|
return debug;
|
2010-07-14 18:36:34 +02:00
|
|
|
return none;
|
|
|
|
}
|
2010-07-14 16:30:03 +02:00
|
|
|
};
|
|
|
|
|
2009-02-19 18:57:27 +01:00
|
|
|
/**
|
2010-03-13 21:49:09 +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 ErrorLogger
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper for error messages, provided by reportErr()
|
|
|
|
*/
|
|
|
|
class ErrorMessage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* File name and line number.
|
2010-07-18 10:20:10 +02:00
|
|
|
* 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 FileLocation
|
|
|
|
{
|
|
|
|
public:
|
2009-03-01 20:38:17 +01:00
|
|
|
FileLocation()
|
|
|
|
{
|
|
|
|
line = 0;
|
|
|
|
}
|
2009-05-31 10:12:19 +02:00
|
|
|
|
2010-07-18 10:20:10 +02:00
|
|
|
/**
|
|
|
|
* Return the filename.
|
|
|
|
* @param convert If true convert path to native separators.
|
|
|
|
* @return filename.
|
|
|
|
*/
|
|
|
|
std::string getfile(bool convert = true) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the filename.
|
|
|
|
* @param file Filename to set.
|
|
|
|
*/
|
2010-07-17 00:27:40 +02:00
|
|
|
void setfile(const std::string &file);
|
2009-02-19 18:57:27 +01:00
|
|
|
unsigned int line;
|
2010-07-17 00:27:40 +02:00
|
|
|
private:
|
|
|
|
std::string _file;
|
|
|
|
|
2009-02-19 18:57:27 +01:00
|
|
|
};
|
|
|
|
|
2010-07-14 22:11:32 +02:00
|
|
|
ErrorMessage(const std::list<FileLocation> &callStack, Severity::SeverityType severity, const std::string &msg, const std::string &id);
|
2009-02-19 23:21:18 +01:00
|
|
|
ErrorMessage();
|
2010-11-11 19:54:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Format the error message in XML format
|
|
|
|
* @param verbose use verbose message
|
2010-12-15 18:45:53 +01:00
|
|
|
* @param ver XML version
|
2010-11-11 19:54:43 +01:00
|
|
|
*/
|
2010-12-02 17:32:51 +01:00
|
|
|
std::string toXML(bool verbose, int ver) const;
|
2009-06-15 20:36:39 +02:00
|
|
|
|
2010-12-02 17:32:51 +01:00
|
|
|
static std::string getXMLHeader(int ver);
|
2009-06-15 20:36:39 +02:00
|
|
|
static std::string getXMLFooter();
|
|
|
|
|
2009-09-05 21:01:49 +02:00
|
|
|
/**
|
|
|
|
* Format the error message into a string.
|
2010-11-11 19:54:43 +01:00
|
|
|
* @param verbose use verbose message
|
2009-09-05 21:01:49 +02:00
|
|
|
* @param outputFormat Empty string to use default output format
|
|
|
|
* or template to be used. E.g. "{file}:{line},{severity},{id},{message}"
|
|
|
|
*/
|
2010-11-11 19:54:43 +01:00
|
|
|
std::string toString(bool verbose, const std::string &outputFormat = "") const;
|
|
|
|
|
|
|
|
std::string serialize() const;
|
|
|
|
bool deserialize(const std::string &data);
|
|
|
|
|
|
|
|
std::list<FileLocation> _callStack;
|
|
|
|
Severity::SeverityType _severity;
|
|
|
|
std::string _id;
|
|
|
|
|
|
|
|
/** source file (not header) */
|
|
|
|
std::string file0;
|
2009-09-05 21:01:49 +02:00
|
|
|
|
2010-11-11 19:54:43 +01:00
|
|
|
/** set short and verbose messages */
|
|
|
|
void setmsg(const std::string &msg);
|
|
|
|
|
|
|
|
/** Short message (single line short message) */
|
|
|
|
const std::string &shortMessage() const
|
|
|
|
{
|
|
|
|
return _shortMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Verbose message (may be the same as the short message) */
|
|
|
|
const std::string &verboseMessage() const
|
|
|
|
{
|
|
|
|
return _verboseMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2009-09-05 21:01:49 +02:00
|
|
|
/**
|
2010-12-15 18:45:53 +01:00
|
|
|
* Replace all occurrences of searchFor with replaceWith in the
|
2009-09-05 21:01:49 +02:00
|
|
|
* 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
|
|
|
|
2010-11-11 19:54:43 +01:00
|
|
|
/** Short message */
|
|
|
|
std::string _shortMessage;
|
|
|
|
|
|
|
|
/** Verbose message */
|
|
|
|
std::string _verboseMessage;
|
2009-02-19 18:57:27 +01:00
|
|
|
};
|
|
|
|
|
2010-08-03 16:36:21 +02:00
|
|
|
ErrorLogger() { }
|
2009-02-19 18:57:27 +01:00
|
|
|
virtual ~ErrorLogger() { }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param msg Location and other information about the found.
|
|
|
|
* error
|
|
|
|
*/
|
|
|
|
virtual void reportErr(const ErrorLogger::ErrorMessage &msg) = 0;
|
|
|
|
|
2009-02-19 23:21:18 +01:00
|
|
|
/**
|
|
|
|
* Information about how many files have been checked
|
|
|
|
*
|
|
|
|
* @param index This many files have been checked.
|
|
|
|
* @param max This many files there are in total.
|
|
|
|
*/
|
|
|
|
virtual void reportStatus(unsigned int index, unsigned int max) = 0;
|
|
|
|
|
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
|
|
|
*/
|
2010-08-06 17:32:16 +02:00
|
|
|
virtual void reportProgress(const std::string &filename, const char stage[], const unsigned int value)
|
2010-08-03 16:36:21 +02:00
|
|
|
{
|
|
|
|
(void)filename;
|
|
|
|
(void)stage;
|
|
|
|
(void)value;
|
|
|
|
}
|
2010-07-24 18:58:52 +02:00
|
|
|
|
2009-07-10 22:38:26 +02:00
|
|
|
static std::string callStackToString(const std::list<ErrorLogger::ErrorMessage::FileLocation> &callStack);
|
2009-02-19 18:57:27 +01:00
|
|
|
};
|
2009-07-13 10:16:31 +02:00
|
|
|
|
|
|
|
|
2009-07-17 10:49:01 +02:00
|
|
|
/// @}
|
|
|
|
|
2009-02-19 18:57:27 +01:00
|
|
|
#endif
|