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) else if (strcmp(argv[i], "--xml") == 0)
_settings->_xml = true; _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 // Only print something when there are errors
else if (strcmp(argv[i], "-q") == 0 || strcmp(argv[i], "--quiet") == 0) else if (strcmp(argv[i], "-q") == 0 || strcmp(argv[i], "--quiet") == 0)
_settings->_errorsOnly = true; _settings->_errorsOnly = true;

View File

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

View File

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

View File

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

View File

@ -129,10 +129,10 @@ bool ErrorLogger::ErrorMessage::deserialize(const std::string &data)
return true; return true;
} }
std::string ErrorLogger::ErrorMessage::getXMLHeader() std::string ErrorLogger::ErrorMessage::getXMLHeader(const std::string &ver)
{ {
return "<?xml version=\"1.0\"?>\n" return "<?xml version=\"1.0\"?>\n"
"<results>"; "<results" + ver + ">";
} }
std::string ErrorLogger::ErrorMessage::getXMLFooter() std::string ErrorLogger::ErrorMessage::getXMLFooter()
@ -161,19 +161,43 @@ static std::string stringToXml(std::string s)
return s; return s;
} }
std::string ErrorLogger::ErrorMessage::toXML(bool verbose) const std::string ErrorLogger::ErrorMessage::toXML(bool verbose, bool xml2) const
{ {
std::ostringstream xml; std::ostringstream xml;
xml << "<error";
if (!_callStack.empty()) if (!xml2)
{ {
xml << " file=\"" << stringToXml(_callStack.back().getfile()) << "\""; xml << "<error";
xml << " line=\"" << _callStack.back().line << "\""; 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 << "\""; else
xml << " severity=\"" << (_severity == Severity::error ? "error" : "style") << "\""; {
xml << " msg=\"" << stringToXml(verbose ? _verboseMessage : _shortMessage) << "\""; xml << " <error";
xml << "/>"; 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(); return xml.str();
} }

View File

@ -124,10 +124,11 @@ public:
/** /**
* Format the error message in XML format * Format the error message in XML format
* @param verbose use verbose message * @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(); static std::string getXMLFooter();
/** /**

View File

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

View File

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

View File

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

View File

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