tools: use clang-format for style checking

Signed-off-by: Marcel Cornu <marcel.d.cornu@intel.com>
This commit is contained in:
Marcel Cornu 2024-04-19 16:51:47 +01:00 committed by Pablo de Lara
parent 7b30857e20
commit 07bca509e7
6 changed files with 120 additions and 26 deletions

46
.clang-format Normal file
View File

@ -0,0 +1,46 @@
# Copyright (c) 2024, Intel Corporation
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of Intel Corporation nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
BasedOnStyle: LLVM
IndentWidth: 8
Language: Cpp
BreakBeforeBraces: Linux
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
UseTab: Never
AlignConsecutiveMacros: true
AlignTrailingComments: true
AlwaysBreakAfterReturnType: All
SortIncludes: false
BreakBeforeInheritanceComma: true
AllowAllParametersOfDeclarationOnNextLine: false
BinPackParameters: true
BinPackArguments: true
ReflowComments: true
ColumnLimit: 100
Cpp11BracedListStyle: false
MaxEmptyLinesToKeep: 1
ContinuationIndentWidth: 8
SpaceAfterCStyleCast: true

2
.clang-format-ignore Normal file
View File

@ -0,0 +1,2 @@
include/aarch64_multibinary.h
**/aarch64/*.h

View File

@ -25,10 +25,10 @@ patches, file issues, and ask questions on our [mailing list].
## Coding Style
The coding style for ISA-L C code roughly follows linux kernel guidelines. Use
the included indent script to format C code.
The coding style for ISA-L C code is roughly based on LLVM style with
some customizations. Use the included format script to format C code.
./tools/iindent your_files.c
./tools/format.sh
And use check format script before submitting.

View File

@ -3,12 +3,23 @@
set -e
rc=0
verbose=0
# NOTE: there is a bug in GNU indent command line parse where it says, that
# -il6 require numeric parameter. This is because it treat it like "-i"
# param. Here we pass -i8 which is default for linux code style and
# we use long parameter name for indent label.
indent_args='-i8 -linux -l95 -cp1 -lps -ncs --indent-label6'
function iver { printf "%03d%03d%03d%03d" $(echo "$@" | sed 's/^.* indent//; y/./ /'); }
clang_format_min_version=18
function clang_format_version() {
version_str=$($clang_format --version)
regex="[0-9]+"
if [[ $version_str =~ $regex ]]; then
major_version="${BASH_REMATCH[0]}"
echo $major_version
fi
}
# set clang-format binary if not set externally
if [[ -z $CLANGFORMAT ]]; then
clang_format="clang-format"
else
clang_format=$CLANGFORMAT
fi
while [ -n "$*" ]; do
case "$1" in
@ -29,26 +40,20 @@ if ! git rev-parse --is-inside-work-tree >& /dev/null; then
exit 1
fi
# On FreeBSD we need to use gindent
for indent_tool in indent gindent ''; do
if hash $indent_tool && [ $(iver $($indent_tool --version)) -ge $(iver 2.2.12) ]; then
break
fi
done
if [ -n "$indent_tool" ]; then
echo "Checking C files for coding style..."
for f in `git ls-files '*.c'`; do
if [ $(clang_format_version) -ge $clang_format_min_version ]; then
echo "Checking C files for coding style (clang-format v$(clang_format_version))..."
for f in `git ls-files '*.[c|h]'`; do
[ "$verbose" -gt 0 ] && echo "checking style on $f"
if ! $indent_tool $indent_args -st $f | diff -q $f - >& /dev/null; then
if ! $clang_format -style=file --dry-run --Werror "$f" >/dev/null 2>&1; then
echo " File found with formatting issues: $f"
[ "$verbose" -gt 0 ] 2> /dev/null && $indent_tool $indent_args -st $f | diff -u $f -
[ "$verbose" -gt 0 ] && $clang_format -style=file --dry-run "$f"
rc=1
fi
done
[ "$rc" -gt 0 ] && echo " Run ./tools/iindent on files"
[ "$rc" -gt 0 ] && echo " Run ./tools/format.sh to fix formatting issues"
else
echo "You do not have a recent indent installed so your code style is not being checked!"
echo "You do not have clang-format version ${clang_format_min_version}+" \
"installed so your code style is not being checked!"
fi
if hash grep; then
@ -74,7 +79,7 @@ while read -r perm _res0 _res1 f; do
echo " File found with permissions issue ($perm): $f"
rc=1
fi
done <<< $(git ls-files -s -- ':(exclude)*.sh' ':(exclude)*iindent')
done <<< $(git ls-files -s -- ':(exclude)*.sh')
echo "Checking script files for permissions..."
while read -r perm _res0 _res1 f; do

43
tools/format.sh Executable file
View File

@ -0,0 +1,43 @@
#!/usr/bin/env bash
verbose=0
clang_format_min_version=18
function clang_format_version() {
version_str=$($clang_format --version)
regex="[0-9]+"
if [[ $version_str =~ $regex ]]; then
major_version="${BASH_REMATCH[0]}"
echo $major_version
fi
}
# set clang-format binary if not set externally
if [[ -z $CLANGFORMAT ]]; then
clang_format="clang-format"
else
clang_format=$CLANGFORMAT
fi
while [ -n "$*" ]; do
case "$1" in
-v )
verbose=1
shift
;;
-h )
echo format.sh [-h -v]
exit 0
;;
esac
done
if [ $(clang_format_version) -ge $clang_format_min_version ]; then
echo "Formatting files using clang-format v$(clang_format_version)..."
for f in `git ls-files '*.[c|h]'`; do
[ "$verbose" -gt 0 ] && echo "formatting $f"
$clang_format -style=file -i "$f"
done
else
echo "clang-format version ${clang_format_min_version}+ is required!"
fi

View File

@ -1,2 +0,0 @@
#!/bin/sh
indent -linux -l95 -cp1 -lps -il6 -ncs "$@"