Skip to content

Commit

Permalink
RHEL-73393: Add format/check options for clang-format-helper
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantin Kostiuk <kkostiuk@redhat.com>
  • Loading branch information
kostyanf14 authored and YanVugenfirer committed Jan 12, 2025
1 parent 18d183e commit e7183eb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
run: |
sudo apt-get update
sudo apt-get install --no-install-recommends -y clang-format-16
bash Tools/clang-format-helper.sh ${CHECK_PATH} "" ${EXCLUDE_REGEX}
bash Tools/clang-format-helper.sh check ${CHECK_PATH} "" ${EXCLUDE_REGEX}
env:
CHECK_PATH: ${{ matrix.path['check'] }}
EXCLUDE_REGEX: ${{ matrix.path['exclude'] }}
44 changes: 26 additions & 18 deletions Tools/clang-format-helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ 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"
FORMAT_ACTION="${1:-"check"}"
CHECK_PATH="${2:-"${SCRIPT_DIR}/.."}"
CLANG_FORMAT_STYLE="${3:-"${CHECK_PATH}/.clang-format"}"
EXCLUDE_REGEX="$4"
INCLUDE_REGEX="$5"

CLANG_FORMAT_STYLE="$(realpath "${CLANG_FORMAT_STYLE}")"

Expand Down Expand Up @@ -60,21 +61,28 @@ for file in $src_files; do
file_path="$(cygpath -w "${file}")"
fi

formatted_code="$("${clang_format}" --style=file:"${CLANG_FORMAT_STYLE}" --verbose --Werror "${file_path}")"
if [[ -z "$formatted_code" ]]; then
continue
fi
if [[ "${FORMAT_ACTION}" == "check" ]]; then
formatted_code="$("${clang_format}" --style=file:"${CLANG_FORMAT_STYLE}" --verbose --Werror "${file_path}")"
if [[ -z "$formatted_code" ]]; then
continue
fi

# The next line is a workaround to avoid the diff on Windows
# The "echo" command replace the \r\n by \n on Windows but only
# for the last trailing newline this cause the diff to fail
# on Windows because the last newline is different
current_file="$(cat "${file}")"
local_format="$(diff <(echo "${current_file}") <(echo "${formatted_code}") || true)"
if [[ -n "${local_format}" ]]; then
echo "The file ${file} is not formatted correctly"
echo "${local_format}"
exit_code=1
# The next line is a workaround to avoid the diff on Windows
# The "echo" command replace the \r\n by \n on Windows but only
# for the last trailing newline this cause the diff to fail
# on Windows because the last newline is different
current_file="$(cat "${file}")"
local_format="$(diff <(echo "${current_file}") <(echo "${formatted_code}") || true)"
if [[ -n "${local_format}" ]]; then
echo "The file ${file} is not formatted correctly"
echo "${local_format}"
exit_code=1
fi
elif [[ "${FORMAT_ACTION}" == "format" ]]; then
"${clang_format}" --style=file:"${CLANG_FORMAT_STYLE}" --verbose -i "${file_path}"
else
echo "Unknown action: ${FORMAT_ACTION}"
exit 1
fi
fi
done
Expand Down
17 changes: 6 additions & 11 deletions contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,26 @@ People *love* thorough bug reports.
- Style config file for Windows driver /.clang-format
- Style config file for VirtIO library /VirtIO/.clang-format
* To run code style check locally on Linux or Windows (with MSYS or cygwin) use `Tools/clang-format-helper.sh` helper
- on Linux helper uses `clang-format` from PATH
- on Windows helper uses `clang-format` from EWDK 24H2
- on Linux helper uses `clang-format-16` from PATH (because EWDK Win11 24H2 contains clang-format version 16.0.5)
- on Windows helper uses `clang-format` from EWDK Win11 24H2
- CLI:
- For all Windows drivers
```bash
bash Tools/clang-format-helper.sh '.' '' './VirtIO'
bash Tools/clang-format-helper.sh check '.' '' './VirtIO'
```
- For VirtIO library
```bash
bash Tools/clang-format-helper.sh 'VirtIO' '' ''
bash Tools/clang-format-helper.sh check 'VirtIO' '' ''
```
* Tools/clang-format-helper.sh uses positional arguments
1. Action: `check` or `format`
1. Directory where needs to check format
1. Path to .clang-format file (default: `${1}/.clang-format`)
1. Path to .clang-format file (default: `${2}/.clang-format`)
1. Exclude regexp (default: `^$`)
1. Include regexp (default: `^.*\.((((c|C)(c|pp|xx|\+\+)?$)|((h|H)h?(pp|xx|\+\+)?$)))$`)

- To use default just put '' as argument

* **NOTE**

On Windows clang-format reports problem with several files but changes that needs to be done are not detected by git.

We are investigating this issue.

## HCK\HLK tests
* The contributions should pass Microsoft certification tests. We are running CI to check that the changes in the pull request can pass. If you submit a lot of PRs, you can setup AutoHCK on your premises to test your code changes: [auto-hck setup](https://github.com/HCK-CI/HCK-CI-DOCS/blob/master/installing-hck-ci-from-scratch.txt)

Expand Down

0 comments on commit e7183eb

Please sign in to comment.