Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \
$(libcppdir)/findtoken.o \
$(libcppdir)/forwardanalyzer.o \
$(libcppdir)/fwdanalysis.o \
$(libcppdir)/gitlabcodequalityreport.o \
$(libcppdir)/importproject.o \
$(libcppdir)/infer.o \
$(libcppdir)/keywords.o \
Expand Down Expand Up @@ -320,6 +321,7 @@ TESTOBJ = test/fixture.o \
test/testfrontend.o \
test/testfunctions.o \
test/testgarbage.o \
test/testgitlabcodequalityreport.o \
test/testimportproject.o \
test/testincompletestatement.o \
test/testinternal.o \
Expand Down Expand Up @@ -616,6 +618,9 @@ $(libcppdir)/forwardanalyzer.o: lib/forwardanalyzer.cpp lib/analyzer.h lib/astut
$(libcppdir)/fwdanalysis.o: lib/fwdanalysis.cpp lib/astutils.h lib/checkers.h lib/config.h lib/errortypes.h lib/fwdanalysis.h lib/library.h lib/mathlib.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/fwdanalysis.cpp

$(libcppdir)/gitlabcodequalityreport.o: lib/gitlabcodequalityreport.cpp externals/picojson/picojson.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/gitlabcodequalityreport.h lib/json.h lib/path.h lib/refthunk.h lib/standards.h lib/utils.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/gitlabcodequalityreport.cpp

$(libcppdir)/importproject.o: lib/importproject.cpp externals/picojson/picojson.h externals/tinyxml2/tinyxml2.h lib/checkers.h lib/config.h lib/filesettings.h lib/importproject.h lib/json.h lib/library.h lib/mathlib.h lib/path.h lib/pathmatch.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h lib/xml.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/importproject.cpp

Expand Down Expand Up @@ -703,7 +708,7 @@ frontend/frontend.o: frontend/frontend.cpp frontend/frontend.h lib/checkers.h li
cli/cmdlineparser.o: cli/cmdlineparser.cpp cli/cmdlinelogger.h cli/cmdlineparser.h cli/filelister.h externals/tinyxml2/tinyxml2.h lib/addoninfo.h lib/check.h lib/checkers.h lib/checks.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/pathmatch.h lib/platform.h lib/refthunk.h lib/regex.h lib/rule.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h lib/xml.h
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/cmdlineparser.cpp

cli/cppcheckexecutor.o: cli/cppcheckexecutor.cpp cli/cmdlinelogger.h cli/cmdlineparser.h cli/cppcheckexecutor.h cli/executor.h cli/processexecutor.h cli/sehwrapper.h cli/signalhandler.h cli/singleexecutor.h cli/threadexecutor.h externals/picojson/picojson.h lib/analyzerinfo.h lib/check.h lib/checkers.h lib/checkersreport.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/json.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/sarifreport.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h
cli/cppcheckexecutor.o: cli/cppcheckexecutor.cpp cli/cmdlinelogger.h cli/cmdlineparser.h cli/cppcheckexecutor.h cli/executor.h cli/processexecutor.h cli/sehwrapper.h cli/signalhandler.h cli/singleexecutor.h cli/threadexecutor.h externals/picojson/picojson.h lib/analyzerinfo.h lib/check.h lib/checkers.h lib/checkersreport.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/gitlabcodequalityreport.h lib/json.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/sarifreport.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h
$(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/cppcheckexecutor.cpp

cli/executor.o: cli/executor.cpp cli/executor.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/refthunk.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h
Expand Down Expand Up @@ -820,6 +825,9 @@ test/testfunctions.o: test/testfunctions.cpp lib/check.h lib/checkers.h lib/chec
test/testgarbage.o: test/testgarbage.cpp lib/check.h lib/checkers.h lib/checks.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/smallvector.h lib/standards.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h test/helpers.h
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testgarbage.cpp

test/testgitlabcodequalityreport.o: test/testgitlabcodequalityreport.cpp externals/picojson/picojson.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/gitlabcodequalityreport.h lib/json.h lib/library.h lib/mathlib.h lib/platform.h lib/refthunk.h lib/settings.h lib/standards.h lib/utils.h test/fixture.h
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testgitlabcodequalityreport.cpp

test/testimportproject.o: test/testimportproject.cpp lib/check.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/refthunk.h lib/settings.h lib/standards.h lib/suppressions.h lib/tokenize.h lib/tokenlist.h lib/utils.h test/fixture.h test/helpers.h test/redirect.h
$(CXX) ${INCLUDE_FOR_TEST} ${CFLAGS_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/testimportproject.cpp

Expand Down
5 changes: 4 additions & 1 deletion cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,8 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
mSettings.outputFormat = Settings::OutputFormat::text;
else if (format == "sarif")
mSettings.outputFormat = Settings::OutputFormat::sarif;
else if (format == "gitlab-codequality")
mSettings.outputFormat = Settings::OutputFormat::gitlabCodeQuality;
else if (format == "xml")
mSettings.outputFormat = Settings::OutputFormat::xml;
else if (format == "xmlv2") {
Expand All @@ -1058,7 +1060,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
mSettings.outputFormat = Settings::OutputFormat::xml;
mSettings.xml_version = 3;
} else {
mLogger.printError("argument to '--output-format=' must be 'text', 'sarif', 'xml' (deprecated), 'xmlv2' or 'xmlv3'.");
mLogger.printError("argument to '--output-format=' must be 'text', 'sarif', 'gitlab-codequality', 'xml' (deprecated), 'xmlv2' or 'xmlv3'.");
return Result::Fail;
}
mSettings.plistOutput = "";
Expand Down Expand Up @@ -1913,6 +1915,7 @@ void CmdLineParser::printHelp() const
" Specify the output format. The available formats are:\n"
" * text\n"
" * sarif\n"
" * gitlab-codequality\n"
" * xml (deprecated)\n"
" * xmlv2\n"
" * xmlv3\n"
Expand Down
20 changes: 19 additions & 1 deletion cli/cppcheckexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "errorlogger.h"
#include "errortypes.h"
#include "filesettings.h"
#include "gitlabcodequalityreport.h"
#include "path.h"
#include "sarifreport.h"
#include "settings.h"
Expand Down Expand Up @@ -117,8 +118,17 @@ namespace {
}

~StdLogger() override {
// SARIF and GitlabCodeQuality reports are a JSON array. They cannot
// be written incrementally, but only when the full set of findings
// is available.
if (mSettings.outputFormat == Settings::OutputFormat::sarif) {
reportErr(mSarifReport.serialize(mSettings.cppcheckCfgProductName));
} else if (mSettings.outputFormat == Settings::OutputFormat::gitlabCodeQuality) {
if (mGitlabCodeQualityReport.hasAbsolutePath()) {
// Keep the JSON report on stderr free of warnings.
reportOut("cppcheck: warning: GitLab Code Quality requires repository-relative paths; use --relative-paths=<repository-root>.");
}
reportErr(mGitlabCodeQualityReport.serialize());
}
delete mErrorOutput;
}
Expand Down Expand Up @@ -238,6 +248,11 @@ namespace {
*/
SarifReport mSarifReport;

/**
* GitLab Code Quality report generator
*/
GitlabCodeQualityReport mGitlabCodeQualityReport;

/**
* Coding standard guideline mapping
*/
Expand Down Expand Up @@ -679,7 +694,8 @@ void StdLogger::reportErr(const ErrorMessage &msg)
// Don't perform redundant reads for these formats, the code is not needed
// for deduplication
const bool noCode = mSettings.outputFormat == Settings::OutputFormat::xml ||
mSettings.outputFormat == Settings::OutputFormat::sarif;
mSettings.outputFormat == Settings::OutputFormat::sarif ||
mSettings.outputFormat == Settings::OutputFormat::gitlabCodeQuality;
const std::string msgStr =
msgCopy.toString(mSettings.verbose, mSettings.templateFormat,
mSettings.templateLocation, noCode);
Expand All @@ -690,6 +706,8 @@ void StdLogger::reportErr(const ErrorMessage &msg)

if (mSettings.outputFormat == Settings::OutputFormat::sarif) {
mSarifReport.addFinding(std::move(msgCopy));
} else if (mSettings.outputFormat == Settings::OutputFormat::gitlabCodeQuality) {
mGitlabCodeQualityReport.addFinding(std::move(msgCopy));
} else if (mSettings.outputFormat == Settings::OutputFormat::xml) {
reportErr(msgCopy.toXML());
} else {
Expand Down
2 changes: 2 additions & 0 deletions lib/cppcheck.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<ClCompile Include="findtoken.cpp" />
<ClCompile Include="forwardanalyzer.cpp" />
<ClCompile Include="fwdanalysis.cpp" />
<ClCompile Include="gitlabcodequalityreport.cpp" />
<ClCompile Include="importproject.cpp" />
<ClCompile Include="infer.cpp" />
<ClCompile Include="keywords.cpp" />
Expand Down Expand Up @@ -152,6 +153,7 @@
<ClInclude Include="findtoken.h" />
<ClInclude Include="forwardanalyzer.h" />
<ClInclude Include="fwdanalysis.h" />
<ClInclude Include="gitlabcodequalityreport.h" />
<ClInclude Include="importproject.h" />
<ClInclude Include="infer.h" />
<ClInclude Include="json.h" />
Expand Down
124 changes: 124 additions & 0 deletions lib/gitlabcodequalityreport.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2026 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "gitlabcodequalityreport.h"

#include "errorlogger.h"
#include "errortypes.h"
#include "path.h"
#include "utils.h"

#include <cstdint>
#include <utility>

namespace {
std::string locationPath(const ErrorMessage::FileLocation& location)
{
std::string path = location.getfile(false);
while (startsWith(path, "./"))
path.erase(0, 2);
return path;
}

picojson::object serializeLocation(const ErrorMessage::FileLocation& location)
{
picojson::object lines;
lines["begin"] = picojson::value(static_cast<int64_t>(location.line < 1 ? 1 : location.line));

picojson::object result;
result["path"] = picojson::value(locationPath(location));
result["lines"] = picojson::value(lines);
return result;
}

std::string fingerprint(const ErrorMessage& finding)
{
if (finding.hash != 0) {
return std::to_string(finding.hash);
}

// Findings without token information have no warning hash.
const ErrorMessage::FileLocation& location = finding.callStack.back();
return finding.id + ':' + finding.shortMessage() + ':' + locationPath(location) + ':' +
std::to_string(location.line);
}
}

void GitlabCodeQualityReport::addFinding(ErrorMessage msg)
{
if (!msg.callStack.empty() && Path::isAbsolute(locationPath(msg.callStack.back()))) {
mHasAbsolutePath = true;
}
mFindings.push_back(std::move(msg));
}

picojson::object GitlabCodeQualityReport::serializeFinding(const ErrorMessage& finding)
{
const ErrorMessage::FileLocation& location = finding.callStack.back();

std::string description = finding.shortMessage();
if (finding.cwe.id > 0) {
description += " (CWE-" + std::to_string(finding.cwe.id) + ")";
}

picojson::object result;
result["description"] = picojson::value(description);
result["check_name"] = picojson::value(finding.id);
result["fingerprint"] = picojson::value(fingerprint(finding));
result["severity"] = picojson::value(codeQualitySeverity(finding));
result["location"] = picojson::value(serializeLocation(location));
return result;
}

std::string GitlabCodeQualityReport::serialize() const
{
picojson::array findings;
for (const ErrorMessage& finding : mFindings) {
// GitLab requires a location for every code quality finding.
if (finding.callStack.empty()) {
continue;
}
findings.emplace_back(serializeFinding(finding));
}

return picojson::value(findings).serialize(true);
}

std::string GitlabCodeQualityReport::codeQualitySeverity(const ErrorMessage& finding)
{
if (ErrorLogger::isCriticalErrorId(finding.id)) {
return "critical";
}

switch (finding.severity) {
case Severity::error:
return "critical";
case Severity::warning:
return "major";
case Severity::style:
case Severity::performance:
case Severity::portability:
return "minor";
case Severity::information:
case Severity::internal:
case Severity::debug:
case Severity::none:
return "info";
}
return "info";
}
84 changes: 84 additions & 0 deletions lib/gitlabcodequalityreport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2026 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef GITLAB_CODE_QUALITY_REPORT_H
#define GITLAB_CODE_QUALITY_REPORT_H

#include "config.h"
#include "errorlogger.h"

#include <string>
#include <vector>

#include "json.h"

/**
* @brief Generates a GitLab Code Quality report.
*
* The report is a JSON array in the GitLab Code Quality report format.
*
* Each finding must contain:
*
* - description: human-readable description of the violation
* - check_name: unique name of the check or rule
* - fingerprint: unique fingerprint for the violation
* - severity: info, minor, major, critical, or blocker
* - location.path: repository-relative path to the source file
* - location.lines.begin (or location.positions.begin.line): line number
*
* Example:
* @code{.json}
* [
* {
* "description": "Division by zero. (CWE-369)",
* "check_name": "zerodiv",
* "fingerprint": "12532254259410460725",
* "severity": "critical",
* "location": {
* "path": "src/main.cpp",
* "lines": {
* "begin": 4
* }
* }
* }
* ]
* @endcode
*
* @see https://docs.gitlab.com/ci/testing/code_quality/#code-quality-report-format
*/
class CPPCHECKLIB GitlabCodeQualityReport {
public:
GitlabCodeQualityReport() = default;
~GitlabCodeQualityReport() = default;

void addFinding(ErrorMessage msg);
std::string serialize() const;

bool hasAbsolutePath() const {
return mHasAbsolutePath;
}

private:
static picojson::object serializeFinding(const ErrorMessage& finding);
static std::string codeQualitySeverity(const ErrorMessage& finding);

std::vector<ErrorMessage> mFindings;
bool mHasAbsolutePath{};
};

#endif // GITLAB_CODE_QUALITY_REPORT_H
2 changes: 1 addition & 1 deletion lib/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ class CPPCHECKLIB WARN_UNUSED Settings {
/** @brief write results (--output-file=&lt;file&gt;) */
std::string outputFile;

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

Platform platform;
Expand Down
4 changes: 4 additions & 0 deletions oss-fuzz/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ LIBOBJ = $(libcppdir)/valueflow.o \
$(libcppdir)/findtoken.o \
$(libcppdir)/forwardanalyzer.o \
$(libcppdir)/fwdanalysis.o \
$(libcppdir)/gitlabcodequalityreport.o \
$(libcppdir)/importproject.o \
$(libcppdir)/infer.o \
$(libcppdir)/keywords.o \
Expand Down Expand Up @@ -285,6 +286,9 @@ $(libcppdir)/forwardanalyzer.o: ../lib/forwardanalyzer.cpp ../lib/analyzer.h ../
$(libcppdir)/fwdanalysis.o: ../lib/fwdanalysis.cpp ../lib/astutils.h ../lib/checkers.h ../lib/config.h ../lib/errortypes.h ../lib/fwdanalysis.h ../lib/library.h ../lib/mathlib.h ../lib/platform.h ../lib/refthunk.h ../lib/settings.h ../lib/smallvector.h ../lib/sourcelocation.h ../lib/standards.h ../lib/symboldatabase.h ../lib/templatesimplifier.h ../lib/token.h ../lib/utils.h ../lib/vfvalue.h
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/fwdanalysis.cpp

$(libcppdir)/gitlabcodequalityreport.o: ../lib/gitlabcodequalityreport.cpp ../externals/picojson/picojson.h ../lib/config.h ../lib/errorlogger.h ../lib/errortypes.h ../lib/gitlabcodequalityreport.h ../lib/json.h ../lib/path.h ../lib/refthunk.h ../lib/standards.h ../lib/utils.h
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/gitlabcodequalityreport.cpp

$(libcppdir)/importproject.o: ../lib/importproject.cpp ../externals/picojson/picojson.h ../externals/tinyxml2/tinyxml2.h ../lib/checkers.h ../lib/config.h ../lib/filesettings.h ../lib/importproject.h ../lib/json.h ../lib/library.h ../lib/mathlib.h ../lib/path.h ../lib/pathmatch.h ../lib/platform.h ../lib/settings.h ../lib/standards.h ../lib/suppressions.h ../lib/utils.h ../lib/xml.h
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/importproject.cpp

Expand Down
1 change: 1 addition & 0 deletions releasenotes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Options:
- Rules: The PCRE library that we used is end-of-life. We have switched to PCRE2 instead. Existing rule files
will hopefully work without changes.
- The --clang option has been deprecated, it will be removed in Cppcheck 2.27
- New output format: --output-format=gitlab-codequality for GitLab Code Quality reports.

Major bug fixes & crashes:
-
Expand Down
Loading
Loading