scripts: ci: check_compliance.py: Add clang-format check

Add a new compliance check that reports any clang-format issues on
the git diff and prints a warning.

Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
This commit is contained in:
Pieter De Gendt 2024-06-27 11:52:52 +02:00 committed by Anas Nashif
parent f21c97a9c5
commit 6a101ae962
4 changed files with 53 additions and 7 deletions

View File

@ -38,7 +38,7 @@ jobs:
run: |
pip3 install setuptools
pip3 install wheel
pip3 install python-magic lxml junitparser gitlint pylint pykwalify yamllint
pip3 install python-magic lxml junitparser gitlint pylint pykwalify yamllint clang-format unidiff
pip3 install west
- name: west setup
@ -94,16 +94,23 @@ jobs:
exit 1;
fi
warns=("ClangFormat")
files=($(./scripts/ci/check_compliance.py -l))
for file in "${files[@]}"; do
f="${file}.txt"
if [[ -s $f ]]; then
errors=$(cat $f)
errors="${errors//'%'/'%25'}"
errors="${errors//$'\n'/'%0A'}"
errors="${errors//$'\r'/'%0D'}"
echo "::error file=${f}::$errors"
exit=1
results=$(cat $f)
results="${results//'%'/'%25'}"
results="${results//$'\n'/'%0A'}"
results="${results//$'\r'/'%0D'}"
if [[ "${warns[@]}" =~ "${file}" ]]; then
echo "::warning file=${f}::$results"
else
echo "::error file=${f}::$results"
exit=1
fi
fi
done

1
.gitignore vendored
View File

@ -76,6 +76,7 @@ tags
BinaryFiles.txt
BoardYml.txt
Checkpatch.txt
ClangFormat.txt
DevicetreeBindings.txt
GitDiffCheck.txt
Gitlint.txt

View File

@ -19,6 +19,7 @@ import traceback
import shlex
import shutil
import textwrap
import unidiff
from yamllint import config, linter
@ -262,6 +263,41 @@ class BoardYmlCheck(ComplianceTest):
for file in path.glob("**/board.yml"):
self.check_board_file(file, vendor_prefixes)
class ClangFormatCheck(ComplianceTest):
"""
Check if clang-format reports any issues
"""
name = "ClangFormat"
doc = "See https://docs.zephyrproject.org/latest/contribute/guidelines.html#clang-format for more details."
path_hint = "<git-top>"
def run(self):
for file in get_files():
diff = subprocess.Popen(('git', 'diff', '-U0', '--no-color', COMMIT_RANGE, '--', file),
stdout=subprocess.PIPE,
cwd=GIT_TOP)
try:
subprocess.run(('clang-format-diff.py', '-p1'),
check=True,
stdin=diff.stdout,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=GIT_TOP)
except subprocess.CalledProcessError as ex:
patchset = unidiff.PatchSet.from_string(ex.output, encoding="utf-8")
for patch in patchset:
for hunk in patch:
# Strip the before and after context
msg = "".join([str(l) for l in hunk[3:-3]])
# show the hunk at the last line
self.fmtd_failure("notice",
"You may want to run clang-format on this change",
file, line=hunk.source_start + hunk.source_length - 3,
desc=f'\r\n{msg}')
class DevicetreeBindingsCheck(ComplianceTest):
"""
Checks if we are introducing any unwanted properties in Devicetree Bindings.

View File

@ -1,9 +1,11 @@
# COMPLIANCE: required by the compliance scripts
# used by ci/check_compliance
clang-format
python-magic
python-magic-bin; sys_platform == "win32"
lxml
junitparser>=2
pylint>=3
unidiff
yamllint