Removed XML version 1

This commit is contained in:
Daniel Marjamäki 2017-07-29 18:56:22 +02:00
parent d1dfe21121
commit 91eb0f1fad
9 changed files with 57 additions and 147 deletions

View File

@ -299,9 +299,9 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
return false;
}
if (_settings->xml_version < 0 || _settings->xml_version > 2) {
// We only have xml versions 1 and 2
PrintMessage("cppcheck: '--xml-version' can only be 1 or 2.");
if (_settings->xml_version != 2) {
// We only have xml version 2
PrintMessage("cppcheck: '--xml-version' can only be 2.");
return false;
}
@ -772,14 +772,6 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
PrintMessage("cppcheck: unusedFunction check can't be used with '-j' option. Disabling unusedFunction check.");
}
if (_settings->xml) {
// Warn about XML format 1, which will be removed in cppcheck 1.81
if (_settings->xml_version == 1U)
PrintMessage("cppcheck: XML format version 1 is deprecated and will be removed in cppcheck 1.81. Use '--xml-version=2'.");
if (_settings->inconclusive && _settings->xml_version == 1U)
PrintMessage("cppcheck: inconclusive messages will not be shown, because the old xml format is not compatible.");
}
if (argc <= 1) {
_showHelp = true;
_exitAfterPrint = true;

View File

@ -105,9 +105,9 @@ bool CppCheckExecutor::parseFromArgs(CppCheck *cppcheck, int argc, const char* c
if (parser.GetShowErrorMessages()) {
errorlist = true;
std::cout << ErrorLogger::ErrorMessage::getXMLHeader(settings.xml_version);
std::cout << ErrorLogger::ErrorMessage::getXMLHeader();
cppcheck->getErrorMessages();
std::cout << ErrorLogger::ErrorMessage::getXMLFooter(settings.xml_version) << std::endl;
std::cout << ErrorLogger::ErrorMessage::getXMLFooter() << std::endl;
}
if (parser.ExitAfterPrinting()) {
@ -830,7 +830,7 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const cha
}
if (settings.xml) {
reportErr(ErrorLogger::ErrorMessage::getXMLHeader(settings.xml_version));
reportErr(ErrorLogger::ErrorMessage::getXMLHeader());
}
if (!settings.buildDir.empty()) {
@ -927,7 +927,7 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck, int /*argc*/, const cha
}
if (settings.xml) {
reportErr(ErrorLogger::ErrorMessage::getXMLFooter(settings.xml_version));
reportErr(ErrorLogger::ErrorMessage::getXMLFooter());
}
_settings = 0;
@ -998,9 +998,9 @@ void CppCheckExecutor::reportStatus(std::size_t fileindex, std::size_t filecount
void CppCheckExecutor::reportErr(const ErrorLogger::ErrorMessage &msg)
{
if (errorlist) {
reportOut(msg.toXML(false, _settings->xml_version));
reportOut(msg.toXML());
} else if (_settings->xml) {
reportErr(msg.toXML(_settings->verbose, _settings->xml_version));
reportErr(msg.toXML());
} else {
reportErr(msg.toString(_settings->verbose, _settings->outputFormat));
}

View File

@ -148,10 +148,10 @@ bool AnalyzerInformation::analyzeFile(const std::string &buildDir, const std::st
return true;
}
void AnalyzerInformation::reportErr(const ErrorLogger::ErrorMessage &msg, bool verbose)
void AnalyzerInformation::reportErr(const ErrorLogger::ErrorMessage &msg, bool /*verbose*/)
{
if (fout.is_open())
fout << msg.toXML(verbose,2) << '\n';
fout << msg.toXML() << '\n';
}
void AnalyzerInformation::setFileInfo(const std::string &check, const std::string &fileInfo)

View File

@ -38,7 +38,7 @@ Check::Check(const std::string &aname)
void Check::reportError(const ErrorLogger::ErrorMessage &errmsg)
{
std::cout << errmsg.toXML(true, 1) << std::endl;
std::cout << errmsg.toXML() << std::endl;
}
bool Check::wrongData(const Token *tok, bool condition, const char *str)

View File

@ -287,7 +287,7 @@ bool ErrorLogger::ErrorMessage::deserialize(const std::string &data)
return true;
}
std::string ErrorLogger::ErrorMessage::getXMLHeader(int xml_version)
std::string ErrorLogger::ErrorMessage::getXMLHeader()
{
// xml_version 1 is the default xml format
@ -298,21 +298,19 @@ std::string ErrorLogger::ErrorMessage::getXMLHeader(int xml_version)
// header
printer.OpenElement("results", false);
// version 2 header
if (xml_version == 2) {
printer.PushAttribute("version", xml_version);
printer.OpenElement("cppcheck", false);
printer.PushAttribute("version", CppCheck::version());
printer.CloseElement(false);
printer.OpenElement("errors", false);
}
printer.PushAttribute("version", 2);
printer.OpenElement("cppcheck", false);
printer.PushAttribute("version", CppCheck::version());
printer.CloseElement(false);
printer.OpenElement("errors", false);
return std::string(printer.CStr()) + '>';
}
std::string ErrorLogger::ErrorMessage::getXMLFooter(int xml_version)
std::string ErrorLogger::ErrorMessage::getXMLFooter()
{
return (xml_version<=1) ? "</results>" : " </errors>\n</results>";
return " </errors>\n</results>";
}
// There is no utf-8 support around but the strings should at least be safe for to tinyxml2.
@ -337,55 +335,31 @@ std::string ErrorLogger::ErrorMessage::fixInvalidChars(const std::string& raw)
return result;
}
std::string ErrorLogger::ErrorMessage::toXML(bool verbose, int version) const
std::string ErrorLogger::ErrorMessage::toXML() const
{
// The default xml format
if (version == 1) {
// No inconclusive messages in the xml version 1
if (_inconclusive)
return "";
tinyxml2::XMLPrinter printer(0, false, 2);
printer.OpenElement("error", false);
printer.PushAttribute("id", _id.c_str());
printer.PushAttribute("severity", Severity::toString(_severity).c_str());
printer.PushAttribute("msg", fixInvalidChars(_shortMessage).c_str());
printer.PushAttribute("verbose", fixInvalidChars(_verboseMessage).c_str());
if (_cwe.id)
printer.PushAttribute("cwe", _cwe.id);
if (_inconclusive)
printer.PushAttribute("inconclusive", "true");
tinyxml2::XMLPrinter printer(0, false, 1);
printer.OpenElement("error", false);
if (!_callStack.empty()) {
printer.PushAttribute("file", _callStack.back().getfile().c_str());
printer.PushAttribute("line", _callStack.back().line);
}
printer.PushAttribute("id", _id.c_str());
printer.PushAttribute("severity", (_severity == Severity::error ? "error" : "style"));
printer.PushAttribute("msg", fixInvalidChars(verbose ? _verboseMessage : _shortMessage).c_str());
for (std::list<FileLocation>::const_reverse_iterator it = _callStack.rbegin(); it != _callStack.rend(); ++it) {
printer.OpenElement("location", false);
if (!file0.empty() && (*it).getfile() != file0)
printer.PushAttribute("file0", Path::toNativeSeparators(file0).c_str());
printer.PushAttribute("file", (*it).getfile().c_str());
printer.PushAttribute("line", (*it).line);
if (!it->getinfo().empty())
printer.PushAttribute("info", it->getinfo().c_str());
printer.CloseElement(false);
return printer.CStr();
}
// The xml format you get when you use --xml-version=2
else if (version == 2) {
tinyxml2::XMLPrinter printer(0, false, 2);
printer.OpenElement("error", false);
printer.PushAttribute("id", _id.c_str());
printer.PushAttribute("severity", Severity::toString(_severity).c_str());
printer.PushAttribute("msg", fixInvalidChars(_shortMessage).c_str());
printer.PushAttribute("verbose", fixInvalidChars(_verboseMessage).c_str());
if (_cwe.id)
printer.PushAttribute("cwe", _cwe.id);
if (_inconclusive)
printer.PushAttribute("inconclusive", "true");
for (std::list<FileLocation>::const_reverse_iterator it = _callStack.rbegin(); it != _callStack.rend(); ++it) {
printer.OpenElement("location", false);
if (!file0.empty() && (*it).getfile() != file0)
printer.PushAttribute("file0", Path::toNativeSeparators(file0).c_str());
printer.PushAttribute("file", (*it).getfile().c_str());
printer.PushAttribute("line", (*it).line);
if (!it->getinfo().empty())
printer.PushAttribute("info", it->getinfo().c_str());
printer.CloseElement(false);
}
printer.CloseElement(false);
return printer.CStr();
}
return "";
printer.CloseElement(false);
return printer.CStr();
}
void ErrorLogger::ErrorMessage::findAndReplace(std::string &source, const std::string &searchFor, const std::string &replaceWith)

View File

@ -247,13 +247,11 @@ public:
/**
* Format the error message in XML format
* @param verbose use verbose message
* @param version XML version
*/
std::string toXML(bool verbose, int version) const;
std::string toXML() const;
static std::string getXMLHeader(int xml_version);
static std::string getXMLFooter(int xml_version);
static std::string getXMLHeader();
static std::string getXMLFooter();
/**
* Format the error message into a string.

View File

@ -37,7 +37,7 @@ Settings::Settings()
verbose(false),
force(false),
relativePaths(false),
xml(false), xml_version(1),
xml(false), xml_version(2),
jobs(1),
loadAverage(0),
exitCode(0),

View File

@ -118,7 +118,6 @@ private:
TEST_CASE(templatesVs);
TEST_CASE(templatesEdit);
TEST_CASE(xml);
TEST_CASE(xmlver1);
TEST_CASE(xmlver2);
TEST_CASE(xmlver2both);
TEST_CASE(xmlver2both2);
@ -834,16 +833,6 @@ private:
ASSERT_EQUALS(1, settings.xml_version);
}
void xmlver1() {
REDIRECT;
const char *argv[] = {"cppcheck", "--xml-version=1", "file.cpp"};
settings.xml_version = 1;
settings.xml = false;
ASSERT(defParser.ParseFromArgs(3, argv));
ASSERT(settings.xml);
ASSERT_EQUALS(1, settings.xml_version);
}
void xmlver2() {
REDIRECT;
const char *argv[] = {"cppcheck", "--xml-version=2", "file.cpp"};

View File

@ -46,10 +46,6 @@ private:
TEST_CASE(CustomFormat);
TEST_CASE(CustomFormat2);
TEST_CASE(CustomFormatLocations);
TEST_CASE(ToXml);
TEST_CASE(ToXmlLocations);
TEST_CASE(ToVerboseXml);
TEST_CASE(ToVerboseXmlLocations);
TEST_CASE(ToXmlV2);
TEST_CASE(ToXmlV2Locations);
TEST_CASE(ToXmlV2Encoding);
@ -190,42 +186,6 @@ private:
ASSERT_EQUALS("Verbose error - bar.cpp(8):(error,errorId)", msg.toString(true, "{message} - {file}({line}):({severity},{id})"));
}
void ToXml() const {
std::list<ErrorLogger::ErrorMessage::FileLocation> locs(1, fooCpp5);
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false);
ASSERT_EQUALS("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<results>", ErrorLogger::ErrorMessage::getXMLHeader(1));
ASSERT_EQUALS("</results>", ErrorLogger::ErrorMessage::getXMLFooter(1));
ASSERT_EQUALS(" <error file=\"foo.cpp\" line=\"5\" id=\"errorId\" severity=\"error\" msg=\"Programming error.\"/>", msg.toXML(false,1));
}
void ToXmlLocations() const {
std::list<ErrorLogger::ErrorMessage::FileLocation> locs;
locs.push_back(fooCpp5);
locs.push_back(barCpp8);
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false);
ASSERT_EQUALS("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<results>", ErrorLogger::ErrorMessage::getXMLHeader(1));
ASSERT_EQUALS("</results>", ErrorLogger::ErrorMessage::getXMLFooter(1));
ASSERT_EQUALS(" <error file=\"bar.cpp\" line=\"8\" id=\"errorId\" severity=\"error\" msg=\"Programming error.\"/>", msg.toXML(false,1));
}
void ToVerboseXml() const {
std::list<ErrorLogger::ErrorMessage::FileLocation> locs(1, fooCpp5);
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false);
ASSERT_EQUALS("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<results>", ErrorLogger::ErrorMessage::getXMLHeader(1));
ASSERT_EQUALS("</results>", ErrorLogger::ErrorMessage::getXMLFooter(1));
ASSERT_EQUALS(" <error file=\"foo.cpp\" line=\"5\" id=\"errorId\" severity=\"error\" msg=\"Verbose error\"/>", msg.toXML(true,1));
}
void ToVerboseXmlLocations() const {
std::list<ErrorLogger::ErrorMessage::FileLocation> locs;
locs.push_back(fooCpp5);
locs.push_back(barCpp8);
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false);
ASSERT_EQUALS("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<results>", ErrorLogger::ErrorMessage::getXMLHeader(1));
ASSERT_EQUALS("</results>", ErrorLogger::ErrorMessage::getXMLFooter(1));
ASSERT_EQUALS(" <error file=\"bar.cpp\" line=\"8\" id=\"errorId\" severity=\"error\" msg=\"Verbose error\"/>", msg.toXML(true,1));
}
void ToXmlV2() const {
std::list<ErrorLogger::ErrorMessage::FileLocation> locs(1, fooCpp5);
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nVerbose error", "errorId", false);
@ -233,12 +193,12 @@ private:
header += " <cppcheck version=\"";
header += CppCheck::version();
header += "\"/>\n <errors>";
ASSERT_EQUALS(header, ErrorLogger::ErrorMessage::getXMLHeader(2));
ASSERT_EQUALS(" </errors>\n</results>", ErrorLogger::ErrorMessage::getXMLFooter(2));
ASSERT_EQUALS(header, ErrorLogger::ErrorMessage::getXMLHeader());
ASSERT_EQUALS(" </errors>\n</results>", ErrorLogger::ErrorMessage::getXMLFooter());
std::string message(" <error id=\"errorId\" severity=\"error\"");
message += " msg=\"Programming error.\" verbose=\"Verbose error\">\n";
message += " <location file=\"foo.cpp\" line=\"5\"/>\n </error>";
ASSERT_EQUALS(message, msg.toXML(false, 2));
ASSERT_EQUALS(message, msg.toXML());
}
void ToXmlV2Locations() const {
@ -250,30 +210,30 @@ private:
header += " <cppcheck version=\"";
header += CppCheck::version();
header += "\"/>\n <errors>";
ASSERT_EQUALS(header, ErrorLogger::ErrorMessage::getXMLHeader(2));
ASSERT_EQUALS(" </errors>\n</results>", ErrorLogger::ErrorMessage::getXMLFooter(2));
ASSERT_EQUALS(header, ErrorLogger::ErrorMessage::getXMLHeader());
ASSERT_EQUALS(" </errors>\n</results>", ErrorLogger::ErrorMessage::getXMLFooter());
std::string message(" <error id=\"errorId\" severity=\"error\"");
message += " msg=\"Programming error.\" verbose=\"Verbose error\">\n";
message += " <location file=\"bar.cpp\" line=\"8\"/>\n";
message += " <location file=\"foo.cpp\" line=\"5\"/>\n </error>";
ASSERT_EQUALS(message, msg.toXML(false, 2));
ASSERT_EQUALS(message, msg.toXML());
}
void ToXmlV2Encoding() const {
{
std::list<ErrorLogger::ErrorMessage::FileLocation> locs;
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error.\nComparing \"\203\" with \"\003\"", "errorId", false);
const std::string message(" <error id=\"errorId\" severity=\"error\" msg=\"Programming error.\" verbose=\"Comparing &quot;\\203&quot; with &quot;\\003&quot;\"/>");
ASSERT_EQUALS(message, msg.toXML(false, 2));
const std::string expected(" <error id=\"errorId\" severity=\"error\" msg=\"Programming error.\" verbose=\"Comparing &quot;\\203&quot; with &quot;\\003&quot;\"/>");
ASSERT_EQUALS(expected, msg.toXML());
}
{
const char code1[]="äöü";
const char code2[]="\x12\x00\x00\x01";
std::list<ErrorLogger::ErrorMessage::FileLocation> locs;
ErrorMessage msg1(locs, emptyString, Severity::error, std::string("Programming error.\nReading \"")+code1+"\"", "errorId", false);
ASSERT_EQUALS(" <error id=\"errorId\" severity=\"error\" msg=\"Programming error.\" verbose=\"Reading &quot;\\303\\244\\303\\266\\303\\274&quot;\"/>", msg1.toXML(false, 2));
ASSERT_EQUALS(" <error id=\"errorId\" severity=\"error\" msg=\"Programming error.\" verbose=\"Reading &quot;\\303\\244\\303\\266\\303\\274&quot;\"/>", msg1.toXML());
ErrorMessage msg2(locs, emptyString, Severity::error, std::string("Programming error.\nReading \"")+code2+"\"", "errorId", false);
ASSERT_EQUALS(" <error id=\"errorId\" severity=\"error\" msg=\"Programming error.\" verbose=\"Reading &quot;\\022&quot;\"/>", msg2.toXML(false, 2));
ASSERT_EQUALS(" <error id=\"errorId\" severity=\"error\" msg=\"Programming error.\" verbose=\"Reading &quot;\\022&quot;\"/>", msg2.toXML());
}
}
@ -284,14 +244,11 @@ private:
// Inconclusive error message
ErrorMessage msg(locs, emptyString, Severity::error, "Programming error", "errorId", true);
// Don't save inconclusive messages if the xml version is 1
ASSERT_EQUALS("", msg.toXML(false, 1));
// xml version 2 error message
ASSERT_EQUALS(" <error id=\"errorId\" severity=\"error\" msg=\"Programming error\" verbose=\"Programming error\" inconclusive=\"true\">\n"
" <location file=\"foo.cpp\" line=\"5\"/>\n"
" </error>",
msg.toXML(false, 2));
msg.toXML());
}
void SerializeInconclusiveMessage() const {