From 12c8eeff2cff16763da7975c54f484742bf8ebe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 1 Dec 2010 21:24:17 +0100 Subject: [PATCH] xml2: Added experimental --xml2 result format. Ticket: #2106 --- cli/cmdlineparser.cpp | 6 ++++++ cli/cppcheckexecutor.cpp | 12 +++++++---- lib/check.h | 2 +- lib/cppcheck.cpp | 2 +- lib/errorlogger.cpp | 46 ++++++++++++++++++++++++++++++---------- lib/errorlogger.h | 5 +++-- lib/preprocessor.cpp | 4 ++-- lib/settings.cpp | 1 + lib/settings.h | 3 +++ test/testcppcheck.cpp | 6 +++--- 10 files changed, 63 insertions(+), 24 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 913545f6d..c99739b86 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -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; diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index 54828fbe0..27b05bdbf 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -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 { diff --git a/lib/check.h b/lib/check.h index a17c9ca3c..a53a2dd5d 100644 --- a/lib/check.h +++ b/lib/check.h @@ -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: diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 488288816..0ebb8e4bb 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -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::iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) { (*it)->getErrorMessages(); diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index e934c8147..3d619b642 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -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 "\n" - ""; + ""; } 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 << ""; } - xml << " id=\"" << _id << "\""; - xml << " severity=\"" << (_severity == Severity::error ? "error" : "style") << "\""; - xml << " msg=\"" << stringToXml(verbose ? _verboseMessage : _shortMessage) << "\""; - xml << "/>"; + else + { + xml << " " << std::endl; + + for (std::list::const_reverse_iterator it = _callStack.rbegin(); it != _callStack.rend(); ++it) + { + xml << " " << std::endl; + } + + xml << " "; + } + return xml.str(); } diff --git a/lib/errorlogger.h b/lib/errorlogger.h index d67292328..f5192d211 100644 --- a/lib/errorlogger.h +++ b/lib/errorlogger.h @@ -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(); /** diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 6995a018a..2751c2bc8 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -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; } diff --git a/lib/settings.cpp b/lib/settings.cpp index fbd85f922..16671f03d 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -35,6 +35,7 @@ Settings::Settings() _verbose = false; _force = false; _xml = false; + _xml2 = false; _jobs = 1; _exitCode = 0; _showtime = 0; // TODO: use enum diff --git a/lib/settings.h b/lib/settings.h index 7dbcd5ff9..879ba44ea 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -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; diff --git a/test/testcppcheck.cpp b/test/testcppcheck.cpp index d2c84458e..4863fa0d2 100644 --- a/test/testcppcheck.cpp +++ b/test/testcppcheck.cpp @@ -389,7 +389,7 @@ private: // Test the errorlogger.. ErrorLogger::ErrorMessage errorMessage; errorMessage.setmsg("abef"); - ASSERT_EQUALS("", errorMessage.toXML(false)); + ASSERT_EQUALS("", 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("", errorMessage.toXML(false)); + ASSERT_EQUALS("", 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("", errorMessage.toXML(false)); + ASSERT_EQUALS("", 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}}")); }