Skip to content

Commit

Permalink
add --output-format option
Browse files Browse the repository at this point in the history
  • Loading branch information
danmar committed Oct 6, 2024
1 parent 5d7fc36 commit 659e028
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 10 deletions.
23 changes: 19 additions & 4 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,20 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
else if (std::strncmp(argv[i], "--output-file=", 14) == 0)
mSettings.outputFile = Path::simplifyPath(argv[i] + 14);

else if (std::strncmp(argv[i], "--output-format=", 16) == 0) {
const std::string format = argv[i] + 16;
if (format == "sarif")
mSettings.outputFormat = Settings::OutputFormat::sarif;
else if (format == "xml")
mSettings.outputFormat = Settings::OutputFormat::xml;
else {
mLogger.printError("argument to '--output-format=' must be 'sarif' or 'xml'.");
return Result::Fail;
}
mSettings.xml = (mSettings.outputFormat == Settings::OutputFormat::xml);
}


// Experimental: limit execution time for extended valueflow analysis. basic valueflow analysis
// is always executed.
else if (std::strncmp(argv[i], "--performance-valueflow-max-time=", 33) == 0) {
Expand Down Expand Up @@ -951,6 +965,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a

// Write results in results.plist
else if (std::strncmp(argv[i], "--plist-output=", 15) == 0) {
mSettings.outputFormat = Settings::OutputFormat::plist;
mSettings.plistOutput = Path::simplifyPath(argv[i] + 15);
if (mSettings.plistOutput.empty())
mSettings.plistOutput = ".";
Expand Down Expand Up @@ -1362,12 +1377,11 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
else if (std::strcmp(argv[i], "-v") == 0 || std::strcmp(argv[i], "--verbose") == 0)
mSettings.verbose = true;

else if (std::strcmp(argv[i], "--sarif") == 0)
mSettings.sarif = true;

// Write results in results.xml
else if (std::strcmp(argv[i], "--xml") == 0)
else if (std::strcmp(argv[i], "--xml") == 0) {
mSettings.xml = true;
mSettings.outputFormat = Settings::OutputFormat::xml;
}

// Define the XML file version (and enable XML output)
else if (std::strncmp(argv[i], "--xml-version=", 14) == 0) {
Expand All @@ -1383,6 +1397,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
mSettings.xml_version = tmp;
// Enable also XML if version is set
mSettings.xml = true;
mSettings.outputFormat = Settings::OutputFormat::xml;
}

else {
Expand Down
6 changes: 3 additions & 3 deletions cli/cppcheckexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ namespace {
}

~StdLogger() override {
if (mSettings.sarif) {
std::cerr << mSarifReport.serialize(mSettings.cppcheckCfgProductName);
if (mSettings.outputFormat == Settings::OutputFormat::sarif) {
reportErr(mSarifReport.serialize(mSettings.cppcheckCfgProductName));
}
delete mErrorOutput;
}
Expand Down Expand Up @@ -562,7 +562,7 @@ void StdLogger::reportErr(const ErrorMessage &msg)
if (!mShownErrors.insert(msg.toString(mSettings.verbose)).second)
return;

if (mSettings.sarif)
if (mSettings.outputFormat == Settings::OutputFormat::sarif)
mSarifReport.addFinding(msg);
else if (mSettings.xml)
reportErr(msg.toXML());
Expand Down
6 changes: 3 additions & 3 deletions lib/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ class CPPCHECKLIB WARN_UNUSED Settings {
/** @brief write results (--output-file=&lt;file&gt;) */
std::string outputFile;

enum class OutputFormat {text, plist, sarif, xml};
OutputFormat outputFormat = OutputFormat::text;

Platform platform;

/** @brief pid of cppcheck. Intention is that this is set in the main process. */
Expand Down Expand Up @@ -441,9 +444,6 @@ class CPPCHECKLIB WARN_UNUSED Settings {
/** @brief Is --verbose given? */
bool verbose{};

/** @brief write SARIF results (--sarif) */
bool sarif{};

/** @brief write XML results (--xml) */
bool xml{};

Expand Down
40 changes: 40 additions & 0 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(maxConfigsMissingCount);
TEST_CASE(maxConfigsInvalid);
TEST_CASE(maxConfigsTooSmall);
TEST_CASE(outputFormatSarif);
TEST_CASE(outputFormatXml);
TEST_CASE(outputFormatOther);
TEST_CASE(outputFormatImplicitPlist);
TEST_CASE(outputFormatImplicitXml);
TEST_CASE(premiumOptions1);
TEST_CASE(premiumOptions2);
TEST_CASE(premiumOptions3);
Expand Down Expand Up @@ -1247,6 +1252,41 @@ class TestCmdlineParser : public TestFixture {
ASSERT_EQUALS("cppcheck: error: argument to '--max-configs=' must be greater than 0.\n", logger->str());
}

void outputFormatSarif() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--output-format=sarif", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
ASSERT_EQUALS_ENUM(Settings::OutputFormat::sarif, settings->outputFormat);
}

void outputFormatXml() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--output-format=xml", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
ASSERT_EQUALS_ENUM(Settings::OutputFormat::xml, settings->outputFormat);
}

void outputFormatOther() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--output-format=text", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Fail, parser->parseFromArgs(3, argv));
ASSERT_EQUALS("cppcheck: error: argument to '--output-format=' must be 'sarif' or 'xml'.\n", logger->str());
}

void outputFormatImplicitPlist() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--plist-output=.", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
ASSERT_EQUALS_ENUM(Settings::OutputFormat::plist, settings->outputFormat);
}

void outputFormatImplicitXml() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--xml", "file.cpp"};
ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv));
ASSERT_EQUALS_ENUM(Settings::OutputFormat::xml, settings->outputFormat);
}

void premiumOptions1() {
REDIRECT;
asPremium();
Expand Down

0 comments on commit 659e028

Please sign in to comment.