xml2: Added experimental --xml2 result format. Ticket: #2106

This commit is contained in:
Daniel Marjamäki 2010-12-01 21:24:17 +01:00
parent b4be71aa4e
commit 12c8eeff2c
10 changed files with 63 additions and 24 deletions

View File

@ -142,6 +142,12 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
else if (strcmp(argv[i], "--xml") == 0)
_settings->_xml = true;
#ifndef NDEBUG
// Experimental: Write results in xml2 format
else if (strcmp(argv[i], "--xml2") == 0)
_settings->_xml2 = true;
#endif
// Only print something when there are errors
else if (strcmp(argv[i], "-q") == 0 || strcmp(argv[i], "--quiet") == 0)
_settings->_errorsOnly = true;

View File

@ -95,7 +95,11 @@ int CppCheckExecutor::check(int argc, const char* const argv[])
_settings = cppCheck.settings();
if (_settings._xml)
{
reportErr(ErrorLogger::ErrorMessage::getXMLHeader());
reportErr(ErrorLogger::ErrorMessage::getXMLHeader(""));
}
else if (_settings._xml2)
{
reportErr(ErrorLogger::ErrorMessage::getXMLHeader(" version=\"2\""));
}
unsigned int returnValue = 0;
@ -117,7 +121,7 @@ int CppCheckExecutor::check(int argc, const char* const argv[])
returnValue = executor.check();
}
if (_settings._xml)
if (_settings._xml || _settings._xml2)
{
reportErr(ErrorLogger::ErrorMessage::getXMLFooter());
}
@ -182,9 +186,9 @@ void CppCheckExecutor::reportStatus(unsigned int index, unsigned int max)
void CppCheckExecutor::reportErr(const ErrorLogger::ErrorMessage &msg)
{
if (_settings._xml)
if (_settings._xml || _settings._xml2)
{
reportErr(msg.toXML(_settings._verbose));
reportErr(msg.toXML(_settings._verbose, _settings._xml2));
}
else
{

View File

@ -110,7 +110,7 @@ public:
*/
static void reportError(const ErrorLogger::ErrorMessage &errmsg)
{
std::cout << errmsg.toXML(true) << std::endl;
std::cout << errmsg.toXML(true, false) << std::endl;
}
protected:

View File

@ -363,7 +363,7 @@ void CppCheck::reportStatus(unsigned int /*index*/, unsigned int /*max*/)
void CppCheck::getErrorMessages()
{
// call all "getErrorMessages" in all registered Check classes
std::cout << ErrorLogger::ErrorMessage::getXMLHeader();
std::cout << ErrorLogger::ErrorMessage::getXMLHeader("");
for (std::list<Check *>::iterator it = Check::instances().begin(); it != Check::instances().end(); ++it)
{
(*it)->getErrorMessages();

View File

@ -129,10 +129,10 @@ bool ErrorLogger::ErrorMessage::deserialize(const std::string &data)
return true;
}
std::string ErrorLogger::ErrorMessage::getXMLHeader()
std::string ErrorLogger::ErrorMessage::getXMLHeader(const std::string &ver)
{
return "<?xml version=\"1.0\"?>\n"
"<results>";
"<results" + ver + ">";
}
std::string ErrorLogger::ErrorMessage::getXMLFooter()
@ -161,19 +161,43 @@ static std::string stringToXml(std::string s)
return s;
}
std::string ErrorLogger::ErrorMessage::toXML(bool verbose) const
std::string ErrorLogger::ErrorMessage::toXML(bool verbose, bool xml2) const
{
std::ostringstream xml;
xml << "<error";
if (!_callStack.empty())
if (!xml2)
{
xml << " file=\"" << stringToXml(_callStack.back().getfile()) << "\"";
xml << " line=\"" << _callStack.back().line << "\"";
xml << "<error";
if (!_callStack.empty())
{
xml << " file=\"" << stringToXml(_callStack.back().getfile()) << "\"";
xml << " line=\"" << _callStack.back().line << "\"";
}
xml << " id=\"" << _id << "\"";
xml << " severity=\"" << (_severity == Severity::error ? "error" : "style") << "\"";
xml << " msg=\"" << stringToXml(verbose ? _verboseMessage : _shortMessage) << "\"";
xml << "/>";
}
xml << " id=\"" << _id << "\"";
xml << " severity=\"" << (_severity == Severity::error ? "error" : "style") << "\"";
xml << " msg=\"" << stringToXml(verbose ? _verboseMessage : _shortMessage) << "\"";
xml << "/>";
else
{
xml << " <error";
xml << " id=\"" << _id << "\"";
xml << " severity=\"" << Severity::toString(_severity) << "\"";
xml << " msg=\"" << stringToXml(_shortMessage) << "\"";
xml << " verbose=\"" << stringToXml(_verboseMessage) << "\"";
xml << ">" << std::endl;
for (std::list<FileLocation>::const_reverse_iterator it = _callStack.rbegin(); it != _callStack.rend(); ++it)
{
xml << " <location";
xml << " file=\"" << stringToXml((*it).getfile()) << "\"";
xml << " line=\"" << (*it).line << "\"";
xml << "/>" << std::endl;
}
xml << " </error>";
}
return xml.str();
}

View File

@ -124,10 +124,11 @@ public:
/**
* Format the error message in XML format
* @param verbose use verbose message
* @param xml2 use xml2 format
*/
std::string toXML(bool verbose) const;
std::string toXML(bool verbose, bool xml2) const;
static std::string getXMLHeader();
static std::string getXMLHeader(const std::string &ver);
static std::string getXMLFooter();
/**

View File

@ -2403,11 +2403,11 @@ void Preprocessor::getErrorMessages(std::ostream &ostr)
Severity::style,
"Include file: \"\" not found.",
"missingInclude");
ostr << errmsg.toXML(false) << std::endl;
ostr << errmsg.toXML(false, false) << std::endl;
const ErrorLogger::ErrorMessage errmsg2(locationList,
Severity::error,
"#error ...",
"preprocessorErrorDirective");
ostr << errmsg2.toXML(false) << std::endl;
ostr << errmsg2.toXML(false, false) << std::endl;
}

View File

@ -35,6 +35,7 @@ Settings::Settings()
_verbose = false;
_force = false;
_xml = false;
_xml2 = false;
_jobs = 1;
_exitCode = 0;
_showtime = 0; // TODO: use enum

View File

@ -87,6 +87,9 @@ public:
/** @brief write xml results (--xml) */
bool _xml;
/** @brief write xml2 results (--xml2) */
bool _xml2;
/** @brief How many processes/threads should do checking at the same
time. Default is 1. (-j N) */
unsigned int _jobs;

View File

@ -389,7 +389,7 @@ private:
// Test the errorlogger..
ErrorLogger::ErrorMessage errorMessage;
errorMessage.setmsg("ab<cd>ef");
ASSERT_EQUALS("<error id=\"\" severity=\"style\" msg=\"ab&lt;cd&gt;ef\"/>", errorMessage.toXML(false));
ASSERT_EQUALS("<error id=\"\" severity=\"style\" msg=\"ab&lt;cd&gt;ef\"/>", errorMessage.toXML(false,false));
}
@ -400,7 +400,7 @@ private:
loc.setfile("ab/cd/../ef.h");
errorMessage._callStack.push_back(loc);
const std::string fname(Path::toNativeSeparators("ab/ef.h"));
ASSERT_EQUALS("<error file=\"" + fname + "\" line=\"0\" id=\"\" severity=\"style\" msg=\"\"/>", errorMessage.toXML(false));
ASSERT_EQUALS("<error file=\"" + fname + "\" line=\"0\" id=\"\" severity=\"style\" msg=\"\"/>", errorMessage.toXML(false,false));
ASSERT_EQUALS("[" + fname + ":0]: ", errorMessage.toString(false));
}
@ -415,7 +415,7 @@ private:
errorMessage._severity = Severity::fromString("error");
errorMessage.setmsg("long testMessage");
const std::string fname(Path::toNativeSeparators("some/{file}file.cpp"));
ASSERT_EQUALS("<error file=\"" + fname + "\" line=\"10\" id=\"testId\" severity=\"error\" msg=\"long testMessage\"/>", errorMessage.toXML(false));
ASSERT_EQUALS("<error file=\"" + fname + "\" line=\"10\" id=\"testId\" severity=\"error\" msg=\"long testMessage\"/>", errorMessage.toXML(false,false));
ASSERT_EQUALS("[" + fname + ":10]: (error) long testMessage", errorMessage.toString(false));
ASSERT_EQUALS("testId-" + fname + ",error.10?{long testMessage}", errorMessage.toString(false, "{id}-{file},{severity}.{line}?{{message}}"));
}