From 320c1898a595b022393da40c01d16eacf2fe8ee5 Mon Sep 17 00:00:00 2001 From: Konstantin Kostiuk Date: Thu, 14 Nov 2024 16:36:30 +0200 Subject: [PATCH] RHEL-65784: CI: Enable code style chacking with clang-format Add empty config for clang-format. Jira: https://issues.redhat.com/browse/RHEL-65784 Signed-off-by: Konstantin Kostiuk --- .clang-format | 1 + .github/workflows/clang-format.yml | 25 +++++++++++++ Tools/clang-format.sh | 58 ++++++++++++++++++++++++++++++ VirtIO/.clang-format | 4 +++ 4 files changed, 88 insertions(+) create mode 100644 .clang-format create mode 100644 .github/workflows/clang-format.yml create mode 100644 Tools/clang-format.sh create mode 100644 VirtIO/.clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 000000000..e3845288a --- /dev/null +++ b/.clang-format @@ -0,0 +1 @@ +DisableFormat: true diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml new file mode 100644 index 000000000..886035e50 --- /dev/null +++ b/.github/workflows/clang-format.yml @@ -0,0 +1,25 @@ +name: clang-format check +on: [push, pull_request] + +jobs: + clang-format: + name: Code style check + runs-on: ubuntu-latest + strategy: + matrix: + path: + - check: '.' + exclude: './VirtIO' + - check: 'VirtIO' + exclude: '' + + steps: + - uses: actions/checkout@v4 + - name: Run clang-format style check for C/C++/Protobuf programs. + run: | + sudo apt-get update + sudo apt-get install --no-install-recommends -y clang-format-18 + bash Tools/clang-format.sh ${CHECK_PATH} "" ${EXCLUDE_REGEX} + env: + CHECK_PATH: ${{ matrix.path['check'] }} + EXCLUDE_REGEX: ${{ matrix.path['exclude'] }} diff --git a/Tools/clang-format.sh b/Tools/clang-format.sh new file mode 100644 index 000000000..bc3ee0637 --- /dev/null +++ b/Tools/clang-format.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash + +set -e + +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +# Default to the root of the repository +CHECK_PATH="${1:-"${SCRIPT_DIR}/.."}" +CLANG_FORMAT_STYLE="${2:-"${CHECK_PATH}/.clang-format"}" +EXCLUDE_REGEX="$3" +INCLUDE_REGEX="$4" + +CLANG_FORMAT_STYLE="$(realpath "${CLANG_FORMAT_STYLE}")" + +echo "Using clang-format version: $(clang-format --version)" +echo "Running clang-format on $(realpath ${CHECK_PATH})" +echo "Using clang-format style file: ${CLANG_FORMAT_STYLE}" + +if [[ -z $EXCLUDE_REGEX ]]; then + EXCLUDE_REGEX="^$" +fi + +# Set the filetype regex if nothing was provided. +# Find all C/C++ files: +# h, H, hpp, hh, h++, hxx +# c, C, cpp, cc, c++, cxx +if [[ -z $INCLUDE_REGEX ]]; then + INCLUDE_REGEX='^.*\.((((c|C)(c|pp|xx|\+\+)?$)|((h|H)h?(pp|xx|\+\+)?$)))$' +fi + +# initialize exit code +exit_code=0 + +cd "${CHECK_PATH}" +src_files=$(find "." -name .git -prune -o -regextype posix-egrep -regex "$INCLUDE_REGEX" -print) + +# check formatting in each source file +IFS=$'\n' # Loop below should separate on new lines, not spaces. +for file in $src_files; do + # Only check formatting if the path doesn't match the regex + if ! [[ ${file} =~ $EXCLUDE_REGEX ]]; then + + formated_code="$(clang-format --style=file:"${CLANG_FORMAT_STYLE}" --verbose --Werror "${file}")" + if [[ -z "$formated_code" ]]; then + continue + fi + + local_format="$(diff <(cat "${file}") <(echo "${formated_code}") || true)" + if [[ -n "${local_format}" ]]; then + echo "The file ${file} is not formatted correctly" + echo "${local_format}" + exit_code=1 + fi + fi +done + +echo "clang-format check finished with exit code: ${exit_code}" +exit $exit_code diff --git a/VirtIO/.clang-format b/VirtIO/.clang-format new file mode 100644 index 000000000..5f8bd136b --- /dev/null +++ b/VirtIO/.clang-format @@ -0,0 +1,4 @@ +IndentWidth: 4 +TabWidth: 4 +UseTab: Never +DisableFormat: true