From 07bca509e7a1d106f04803f32ca1adf763bd8f04 Mon Sep 17 00:00:00 2001 From: Marcel Cornu Date: Fri, 19 Apr 2024 16:51:47 +0100 Subject: [PATCH] tools: use clang-format for style checking Signed-off-by: Marcel Cornu --- .clang-format | 46 ++++++++++++++++++++++++++++++++++++++++++ .clang-format-ignore | 2 ++ CONTRIBUTING.md | 6 +++--- tools/check_format.sh | 47 ++++++++++++++++++++++++------------------- tools/format.sh | 43 +++++++++++++++++++++++++++++++++++++++ tools/iindent | 2 -- 6 files changed, 120 insertions(+), 26 deletions(-) create mode 100644 .clang-format create mode 100644 .clang-format-ignore create mode 100755 tools/format.sh delete mode 100755 tools/iindent diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..0c854c6 --- /dev/null +++ b/.clang-format @@ -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 diff --git a/.clang-format-ignore b/.clang-format-ignore new file mode 100644 index 0000000..8cf373a --- /dev/null +++ b/.clang-format-ignore @@ -0,0 +1,2 @@ +include/aarch64_multibinary.h +**/aarch64/*.h diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fb3dfbf..dffa002 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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. diff --git a/tools/check_format.sh b/tools/check_format.sh index fb39ab5..fe0f829 100755 --- a/tools/check_format.sh +++ b/tools/check_format.sh @@ -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 diff --git a/tools/format.sh b/tools/format.sh new file mode 100755 index 0000000..59490a8 --- /dev/null +++ b/tools/format.sh @@ -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 diff --git a/tools/iindent b/tools/iindent deleted file mode 100755 index 48d2636..0000000 --- a/tools/iindent +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -indent -linux -l95 -cp1 -lps -il6 -ncs "$@"