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:
parent
f21c97a9c5
commit
6a101ae962
4 changed files with 53 additions and 7 deletions
21
.github/workflows/compliance.yml
vendored
21
.github/workflows/compliance.yml
vendored
|
|
@ -38,7 +38,7 @@ jobs:
|
||||||
run: |
|
run: |
|
||||||
pip3 install setuptools
|
pip3 install setuptools
|
||||||
pip3 install wheel
|
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
|
pip3 install west
|
||||||
|
|
||||||
- name: west setup
|
- name: west setup
|
||||||
|
|
@ -94,16 +94,23 @@ jobs:
|
||||||
exit 1;
|
exit 1;
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
warns=("ClangFormat")
|
||||||
files=($(./scripts/ci/check_compliance.py -l))
|
files=($(./scripts/ci/check_compliance.py -l))
|
||||||
|
|
||||||
for file in "${files[@]}"; do
|
for file in "${files[@]}"; do
|
||||||
f="${file}.txt"
|
f="${file}.txt"
|
||||||
if [[ -s $f ]]; then
|
if [[ -s $f ]]; then
|
||||||
errors=$(cat $f)
|
results=$(cat $f)
|
||||||
errors="${errors//'%'/'%25'}"
|
results="${results//'%'/'%25'}"
|
||||||
errors="${errors//$'\n'/'%0A'}"
|
results="${results//$'\n'/'%0A'}"
|
||||||
errors="${errors//$'\r'/'%0D'}"
|
results="${results//$'\r'/'%0D'}"
|
||||||
echo "::error file=${f}::$errors"
|
|
||||||
exit=1
|
if [[ "${warns[@]}" =~ "${file}" ]]; then
|
||||||
|
echo "::warning file=${f}::$results"
|
||||||
|
else
|
||||||
|
echo "::error file=${f}::$results"
|
||||||
|
exit=1
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -76,6 +76,7 @@ tags
|
||||||
BinaryFiles.txt
|
BinaryFiles.txt
|
||||||
BoardYml.txt
|
BoardYml.txt
|
||||||
Checkpatch.txt
|
Checkpatch.txt
|
||||||
|
ClangFormat.txt
|
||||||
DevicetreeBindings.txt
|
DevicetreeBindings.txt
|
||||||
GitDiffCheck.txt
|
GitDiffCheck.txt
|
||||||
Gitlint.txt
|
Gitlint.txt
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ import traceback
|
||||||
import shlex
|
import shlex
|
||||||
import shutil
|
import shutil
|
||||||
import textwrap
|
import textwrap
|
||||||
|
import unidiff
|
||||||
|
|
||||||
from yamllint import config, linter
|
from yamllint import config, linter
|
||||||
|
|
||||||
|
|
@ -262,6 +263,41 @@ class BoardYmlCheck(ComplianceTest):
|
||||||
for file in path.glob("**/board.yml"):
|
for file in path.glob("**/board.yml"):
|
||||||
self.check_board_file(file, vendor_prefixes)
|
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):
|
class DevicetreeBindingsCheck(ComplianceTest):
|
||||||
"""
|
"""
|
||||||
Checks if we are introducing any unwanted properties in Devicetree Bindings.
|
Checks if we are introducing any unwanted properties in Devicetree Bindings.
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
# COMPLIANCE: required by the compliance scripts
|
# COMPLIANCE: required by the compliance scripts
|
||||||
|
|
||||||
# used by ci/check_compliance
|
# used by ci/check_compliance
|
||||||
|
clang-format
|
||||||
python-magic
|
python-magic
|
||||||
python-magic-bin; sys_platform == "win32"
|
python-magic-bin; sys_platform == "win32"
|
||||||
lxml
|
lxml
|
||||||
junitparser>=2
|
junitparser>=2
|
||||||
pylint>=3
|
pylint>=3
|
||||||
|
unidiff
|
||||||
yamllint
|
yamllint
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue