Skip to content

Commit

Permalink
RHEL-65784: CI: Enable code style chacking with clang-format
Browse files Browse the repository at this point in the history
Add empty config for clang-format.

Jira: https://issues.redhat.com/browse/RHEL-65784

Signed-off-by: Konstantin Kostiuk <[email protected]>
  • Loading branch information
kostyanf14 committed Nov 14, 2024
1 parent ff57c6c commit 9ab6543
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DisableFormat: true
25 changes: 25 additions & 0 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
@@ -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: |
apt-get update
apt-get install --no-install-recommends -y clang-format-18
bash Tools/clang-format.sh ${{CHECK_PATHpath}} "" ${{EXCLUDE_REGEX}}
env:
CHECK_PATH: ${{ matrix.path['check'] }}
EXCLUDE_REGEX: ${{ matrix.path['exclude'] }}
58 changes: 58 additions & 0 deletions Tools/clang-format.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions VirtIO/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
IndentWidth: 4
TabWidth: 4
UseTab: Never
DisableFormat: true

0 comments on commit 9ab6543

Please sign in to comment.