Refactor cppCheck function for improved clarity (#1581)

* Refactor cppCheck function for improved clarity

* Fix cppcheck command to specify C++ language

* Fix grep command to check cppcheck report format
This commit is contained in:
Olivier
2025-11-22 23:35:20 +01:00
committed by GitHub
parent 4daf09307e
commit 130091deb8

View File

@@ -1,40 +1,111 @@
#!groovy
def cppCheck(config) {
config.pr.setBuildStatus(config, 'PENDING', 'Toll gate (Code analysis - Cppcheck)', 'Running...', '${BUILD_URL}flowGraphTable/')
// We could consider running Cppcheck for GNU as well, but to avoid so many duplicates, we stick to AVR
sh """#!/bin/bash +x
cd ${config.repository_root}
echo "Doing cppcheck for AVR..."
find . -type f \\( -iname \\*.c -o -iname \\*.cpp -o -iname \\*.ino \\) | cppcheck -j 4 --force --file-list=- --enable=style,portability,performance --platform=.mystools/cppcheck/config/avr.xml --suppressions-list=.mystools/cppcheck/config/suppressions.cfg --includes-file=.mystools/cppcheck/config/includes.cfg --language=c++ --inline-suppr --xml --xml-version=2 2> cppcheck-avr.xml
cppcheck-htmlreport --file="cppcheck-avr.xml" --title="cppcheck-avr" --report-dir=cppcheck-avr_cppcheck_reports --source-dir=."""
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: true,
reportDir: config.repository_root+'cppcheck-avr_cppcheck_reports',
reportFiles: 'index.html', reportName: 'CppCheck AVR', reportTitles: ''])
step([$class: 'ViolationsToGitHubRecorder',
config: [
repositoryName: config.repository_name,
pullRequestId: env.CHANGE_ID,
createCommentWithAllSingleFileComments: true,
createSingleFileComments: true,
commentOnlyChangedContent: true,
keepOldComments: false,
violationConfigs: [[pattern: '.*/cppcheck-avr\\.xml$', parser: 'CPPCHECK', reporter: 'Cppcheck'],]
]
])
// 20200226TK: Adjusted for CppCheck 1.90
ret = sh(returnStatus: true,
script: "#!/bin/bash +e\n"+
"cd ${config.repository_root}\n"+
"grep -q \"0 total\" cppcheck-avr_cppcheck_reports/index.html || exit_code=\$?\n"+
"exit \$((exit_code == 0 ? 0 : 1))")
if (ret == 1) {
config.pr.setBuildStatus(config, 'ERROR', 'Toll gate (Code analysis - Cppcheck)', 'Issues found', '${BUILD_URL}CppCheck_20AVR/index.html')
error 'Terminating due to Cppcheck error'
} else {
config.pr.setBuildStatus(config, 'SUCCESS', 'Toll gate (Code analysis - Cppcheck)', 'Pass', '')
}
def cppCheck(config) {
config.pr.setBuildStatus(
config,
'PENDING',
'Toll gate (Code analysis - Cppcheck)',
'Running...',
"${BUILD_URL}flowGraphTable/"
)
sh """#!/bin/bash
set -euo pipefail
cd "${config.repository_root}"
echo "Doing cppcheck for AVR..."
# Collect C/C++/INO files and run cppcheck
find . -type f \\( -iname '*.c' -o -iname '*.cpp' -o -iname '*.ino' \\) \
| cppcheck -j 4 \
--force \
--file-list=- \
--enable=style,portability,performance \
--platform=.mystools/cppcheck/config/avr.xml \
--suppressions-list=.mystools/cppcheck/config/suppressions.cfg \
--includes-file=.mystools/cppcheck/config/includes.cfg \
--language=c++ \
--inline-suppr \
--xml --xml-version=2 \
2> cppcheck-avr.xml
cppcheck-htmlreport \
--file="cppcheck-avr.xml" \
--title="cppcheck-avr" \
--report-dir=cppcheck-avr_cppcheck_reports \
--source-dir=.
"""
// Ensure repository_root ends with a slash
def root = config.repository_root.endsWith('/') ? config.repository_root : config.repository_root + '/'
publishHTML([
allowMissing: false,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: root + 'cppcheck-avr_cppcheck_reports',
reportFiles: 'index.html',
reportName: 'CppCheck AVR',
reportTitles: ''
])
// Only run ViolationsToGitHubRecorder if we have a PR id
if (env.CHANGE_ID) {
step([
$class: 'ViolationsToGitHubRecorder',
config: [
repositoryName: config.repository_name,
pullRequestId: env.CHANGE_ID,
createCommentWithAllSingleFileComments: true,
createSingleFileComments: true,
commentOnlyChangedContent: true,
keepOldComments: false,
violationConfigs: [[
pattern: '.*/cppcheck-avr\\.xml$',
parser: 'CPPCHECK',
reporter: 'Cppcheck'
]]
]
])
}
def ret = sh(
returnStatus: true,
script: """#!/bin/bash
cd "${config.repository_root}"
if ! [ -f cppcheck-avr_cppcheck_reports/index.html ]; then
echo "cppcheck HTML report not found!"
exit 1
fi
if grep -q "0</td><td>total" cppcheck-avr_cppcheck_reports/index.html; then
exit 0
else
exit 1
fi
"""
)
if (ret == 1) {
config.pr.setBuildStatus(
config,
'ERROR',
'Toll gate (Code analysis - Cppcheck)',
'Issues found',
"${BUILD_URL}CppCheck_20AVR/index.html"
)
error 'Terminating due to Cppcheck error'
} else {
config.pr.setBuildStatus(
config,
'SUCCESS',
'Toll gate (Code analysis - Cppcheck)',
'Pass',
''
)
}
}
return this
return this