Skip to content

Commit 633b4e4

Browse files
authored
fix #13431: consolidate CLI options for XML output format (#7700)
1 parent 7515c3a commit 633b4e4

File tree

3 files changed

+79
-4
lines changed

3 files changed

+79
-4
lines changed

cli/cmdlineparser.cpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
315315
{
316316
mSettings.exename = Path::getCurrentExecutablePath(argv[0]);
317317

318+
bool xmlOptionProvided = false;
319+
bool outputFormatOptionProvided = false;
320+
318321
// default to --check-level=normal from CLI for now
319322
mSettings.setCheckLevel(Settings::CheckLevel::normal);
320323

@@ -1001,6 +1004,10 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
10011004
mSettings.outputFile = Path::simplifyPath(argv[i] + 14);
10021005

10031006
else if (std::strncmp(argv[i], "--output-format=", 16) == 0) {
1007+
if (xmlOptionProvided) {
1008+
outputFormatOptionMixingError();
1009+
return Result::Fail;
1010+
}
10041011
const std::string format = argv[i] + 16;
10051012
// plist can not be handled here because it requires additional data
10061013
if (format == "text")
@@ -1009,11 +1016,18 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
10091016
mSettings.outputFormat = Settings::OutputFormat::sarif;
10101017
else if (format == "xml")
10111018
mSettings.outputFormat = Settings::OutputFormat::xml;
1012-
else {
1013-
mLogger.printError("argument to '--output-format=' must be 'text', 'sarif' or 'xml'.");
1019+
else if (format == "xmlv2") {
1020+
mSettings.outputFormat = Settings::OutputFormat::xml;
1021+
mSettings.xml_version = 2;
1022+
} else if (format == "xmlv3") {
1023+
mSettings.outputFormat = Settings::OutputFormat::xml;
1024+
mSettings.xml_version = 3;
1025+
} else {
1026+
mLogger.printError("argument to '--output-format=' must be 'text', 'sarif', 'xml' (deprecated), 'xmlv2' or 'xmlv3'.");
10141027
return Result::Fail;
10151028
}
10161029
mSettings.plistOutput = "";
1030+
outputFormatOptionProvided = true;
10171031
}
10181032

10191033

@@ -1486,11 +1500,20 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
14861500

14871501
// Write results in results.xml
14881502
else if (std::strcmp(argv[i], "--xml") == 0) {
1503+
if (outputFormatOptionProvided) {
1504+
outputFormatOptionMixingError();
1505+
return Result::Fail;
1506+
}
14891507
mSettings.outputFormat = Settings::OutputFormat::xml;
1508+
xmlOptionProvided = true;
14901509
}
14911510

14921511
// Define the XML file version (and enable XML output)
14931512
else if (std::strncmp(argv[i], "--xml-version=", 14) == 0) {
1513+
if (outputFormatOptionProvided) {
1514+
outputFormatOptionMixingError();
1515+
return Result::Fail;
1516+
}
14941517
int tmp;
14951518
if (!parseNumberArg(argv[i], 14, tmp))
14961519
return Result::Fail;
@@ -1503,6 +1526,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
15031526
mSettings.xml_version = tmp;
15041527
// Enable also XML if version is set
15051528
mSettings.outputFormat = Settings::OutputFormat::xml;
1529+
xmlOptionProvided = true;
15061530
}
15071531

15081532
else {
@@ -1814,7 +1838,9 @@ void CmdLineParser::printHelp() const
18141838
" Specify the output format. The available formats are:\n"
18151839
" * text\n"
18161840
" * sarif\n"
1817-
" * xml\n"
1841+
" * xml (deprecated)\n"
1842+
" * xmlv2\n"
1843+
" * xmlv3\n"
18181844
" --platform=<type>, --platform=<file>\n"
18191845
" Specifies platform specific types and sizes. The\n"
18201846
" available builtin platforms are:\n"
@@ -2149,3 +2175,8 @@ std::list<FileWithDetails> CmdLineParser::filterFiles(const std::vector<std::str
21492175
});
21502176
return files;
21512177
}
2178+
2179+
void CmdLineParser::outputFormatOptionMixingError() const
2180+
{
2181+
mLogger.printError("'--output-format' and '--xml...' may not be used in conjunction.");
2182+
}

cli/cmdlineparser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ class CmdLineParser {
179179
bool mAnalyzeAllVsConfigsSetOnCmdLine = false;
180180
/** @brief Name of the language that is enforced. Empty per default. */
181181
Standards::Language mEnforcedLang{Standards::Language::None};
182+
183+
void outputFormatOptionMixingError() const;
182184
};
183185

184186
/// @}

test/testcmdlineparser.cpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,15 @@ class TestCmdlineParser : public TestFixture {
227227
TEST_CASE(outputFormatText);
228228
TEST_CASE(outputFormatSarif);
229229
TEST_CASE(outputFormatXml);
230+
TEST_CASE(outputFormatXmlv2);
231+
TEST_CASE(outputFormatXmlv3);
230232
TEST_CASE(outputFormatOther);
231233
TEST_CASE(outputFormatImplicitPlist);
232234
TEST_CASE(outputFormatImplicitXml);
233235
TEST_CASE(outputFormatOverridePlist);
236+
TEST_CASE(outputFormatMixed1);
237+
TEST_CASE(outputFormatMixed2);
238+
TEST_CASE(outputFormatMixed3);
234239
TEST_CASE(premiumOptions1);
235240
TEST_CASE(premiumOptions2);
236241
TEST_CASE(premiumOptions3);
@@ -1381,11 +1386,27 @@ class TestCmdlineParser : public TestFixture {
13811386
ASSERT_EQUALS_ENUM(Settings::OutputFormat::xml, settings->outputFormat);
13821387
}
13831388

1389+
void outputFormatXmlv2() {
1390+
REDIRECT;
1391+
const char * const argv[] = {"cppcheck", "--output-format=xmlv2", "file.cpp"};
1392+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
1393+
ASSERT_EQUALS_ENUM(Settings::OutputFormat::xml, settings->outputFormat);
1394+
ASSERT_EQUALS(2, settings->xml_version);
1395+
}
1396+
1397+
void outputFormatXmlv3() {
1398+
REDIRECT;
1399+
const char * const argv[] = {"cppcheck", "--output-format=xmlv3", "file.cpp"};
1400+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parseFromArgs(argv));
1401+
ASSERT_EQUALS_ENUM(Settings::OutputFormat::xml, settings->outputFormat);
1402+
ASSERT_EQUALS(3, settings->xml_version);
1403+
}
1404+
13841405
void outputFormatOther() {
13851406
REDIRECT;
13861407
const char * const argv[] = {"cppcheck", "--output-format=plist", "file.cpp"};
13871408
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Fail, parseFromArgs(argv));
1388-
ASSERT_EQUALS("cppcheck: error: argument to '--output-format=' must be 'text', 'sarif' or 'xml'.\n", logger->str());
1409+
ASSERT_EQUALS("cppcheck: error: argument to '--output-format=' must be 'text', 'sarif', 'xml' (deprecated), 'xmlv2' or 'xmlv3'.\n", logger->str());
13891410
}
13901411

13911412
void outputFormatImplicitPlist() {
@@ -1411,6 +1432,27 @@ class TestCmdlineParser : public TestFixture {
14111432
ASSERT_EQUALS("", settings->plistOutput);
14121433
}
14131434

1435+
void outputFormatMixed1() {
1436+
REDIRECT;
1437+
const char * const argv[] = {"cppcheck", "--xml", "--output-format=xml", "file.cpp"};
1438+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Fail, parseFromArgs(argv));
1439+
ASSERT_EQUALS("cppcheck: error: '--output-format' and '--xml...' may not be used in conjunction.\n", logger->str());
1440+
}
1441+
1442+
void outputFormatMixed2() {
1443+
REDIRECT;
1444+
const char * const argv[] = {"cppcheck", "--output-format=xml", "--xml", "file.cpp"};
1445+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Fail, parseFromArgs(argv));
1446+
ASSERT_EQUALS("cppcheck: error: '--output-format' and '--xml...' may not be used in conjunction.\n", logger->str());
1447+
}
1448+
1449+
void outputFormatMixed3() {
1450+
REDIRECT;
1451+
const char * const argv[] = {"cppcheck", "--xml-version=2", "--output-format=xml", "file.cpp"};
1452+
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Fail, parseFromArgs(argv));
1453+
ASSERT_EQUALS("cppcheck: error: '--output-format' and '--xml...' may not be used in conjunction.\n", logger->str());
1454+
}
1455+
14141456
void premiumOptions1() {
14151457
REDIRECT;
14161458
asPremium();

0 commit comments

Comments
 (0)