From 1da88de93243680676a9e9aba27924ca0d87ba9e Mon Sep 17 00:00:00 2001 From: Kimmo Varis Date: Wed, 2 Feb 2011 14:04:50 +0200 Subject: [PATCH] Imrove --xml-version option parsing. Allow --xml-version parsing to recognize also version 1 and print errors about invalid values. --- cli/cmdlineparser.cpp | 23 ++++++++++++++++++++--- test/testcmdlineparser.cpp | 12 ++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 46e26703d..9c1bf264f 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -210,11 +210,28 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[]) else if (strcmp(argv[i], "--xml") == 0) _settings->_xml = true; - // Write results in xml2 format - else if (strcmp(argv[i], "--xml-version=2") == 0) + // Define the XML file version (and enable XML output) + else if (strncmp(argv[i], "--xml-version=", 14) == 0) { + std::string numberString(argv[i]); + numberString = numberString.substr(14); + + std::istringstream iss(numberString); + if (!(iss >> _settings->_xml_version)) + { + PrintMessage("cppcheck: argument to '--xml-version' is not a number"); + 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."); + return false; + } + + // Enable also XML if version is set _settings->_xml = true; - _settings->_xml_version = 2; } // Only print something when there are errors diff --git a/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index 8ab68a941..ec9561b15 100644 --- a/test/testcmdlineparser.cpp +++ b/test/testcmdlineparser.cpp @@ -81,6 +81,7 @@ private: TEST_CASE(templatesGcc); TEST_CASE(templatesVs); TEST_CASE(xml); + TEST_CASE(xmlver1); TEST_CASE(xmlver2); TEST_CASE(xmlver2both); TEST_CASE(xmlver2both2); @@ -587,6 +588,17 @@ private: ASSERT_EQUALS(1, settings._xml_version); } + void xmlver1() + { + REDIRECT; + const char *argv[] = {"cppcheck", "--xml-version=1", "file.cpp"}; + Settings settings; + CmdLineParser parser(&settings); + ASSERT(parser.ParseFromArgs(3, argv)); + ASSERT(settings._xml); + ASSERT_EQUALS(1, settings._xml_version); + } + void xmlver2() { REDIRECT;