misc: migrate from GNU indent to clang-format

Type: make
Change-Id: I085dcd6fe826da14d456f84a23355310bdc5d1e9
Signed-off-by: Damjan Marion <damarion@cisco.com>
This commit is contained in:
Damjan Marion
2020-12-12 19:09:31 +01:00
parent 44cae54737
commit 942542f7c1
4 changed files with 119 additions and 196 deletions
.clang-formatMakefile
build-root/scripts
extras/scripts

@ -1,38 +1,21 @@
---
AlignEscapedNewlinesLeft: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AlwaysBreakBeforeMultilineStrings: false
BreakBeforeBinaryOperators: false
BreakBeforeTernaryOperators: true
BinPackParameters: true
BreakBeforeBraces: GNU
ColumnLimit: 79
IndentCaseLabels: false
MaxEmptyLinesToKeep: 1
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 60
PenaltyBreakString: 1000
PenaltyBreakFirstLessLess: 120
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerBindsToType: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: Always
SpacesBeforeTrailingComments: 1
SpacesInParentheses: false
SpaceInEmptyParentheses: false
SpacesInCStyleCastParentheses: false
SpaceAfterControlStatementKeyword: true
Cpp11BracedListStyle: true
Standard: Cpp11
# Minimal clang-format version is 8
BasedOnStyle: GNU
UseTab: Always
SpaceAfterCStyleCast: true
SortIncludes: false
IndentWidth: 2
TabWidth: 4
UseTab: Never
IndentFunctionDeclarationAfterType: false
ContinuationIndentWidth: 4
...
AlignConsecutiveMacros: true
BreakBeforeTernaryOperators: false
BreakBeforeBinaryOperators: None
ContinuationIndentWidth: 2
ForEachMacros:
- 'clib_bitmap_foreach'
- 'pool_foreach'
- 'pool_foreach_index'
- 'vec_foreach'
- 'vec_foreach_backwards'
- 'vec_foreach_index'
- 'vec_foreach_index_backwards'
- 'vlib_foreach_rx_tx'

@ -14,6 +14,7 @@
export WS_ROOT=$(CURDIR)
export BR=$(WS_ROOT)/build-root
CCACHE_DIR?=$(BR)/.ccache
SHELL:=/bin/bash
GDB?=gdb
PLATFORM?=vpp
SAMPLE_PLUGIN?=no
@ -64,7 +65,7 @@ endif
DEB_DEPENDS = curl build-essential autoconf automake ccache
DEB_DEPENDS += debhelper dkms git libtool libapr1-dev dh-systemd dh-python
DEB_DEPENDS += libconfuse-dev git-review exuberant-ctags cscope pkg-config
DEB_DEPENDS += lcov chrpath autoconf indent clang-format libnuma-dev
DEB_DEPENDS += lcov chrpath autoconf libnuma-dev
DEB_DEPENDS += python3-all python3-setuptools check
DEB_DEPENDS += libboost-all-dev libffi-dev python3-ply libmbedtls-dev
DEB_DEPENDS += cmake ninja-build uuid-dev python3-jsonschema python3-yaml
@ -77,11 +78,12 @@ LIBFFI=libffi6 # works on all but 20.04 and debian-testing
ifeq ($(OS_VERSION_ID),18.04)
DEB_DEPENDS += python-dev python-all python-pip python-virtualenv
DEB_DEPENDS += libssl-dev
DEB_DEPENDS += clang-9
DEB_DEPENDS += clang-9 clang-format-10
else ifeq ($(OS_VERSION_ID),20.04)
DEB_DEPENDS += python3-virtualenv
DEB_DEPENDS += libssl-dev
DEB_DEPENDS += libelf-dev # for libbpf (af_xdp)
DEB_DEPENDS += clang-format-10
LIBFFI=libffi7
else ifeq ($(OS_ID)-$(OS_VERSION_ID),debian-9)
DEB_DEPENDS += libssl1.0-dev
@ -613,7 +615,10 @@ compdb:
.PHONY: checkstyle
checkstyle: checkfeaturelist
@build-root/scripts/checkstyle.sh
ifeq ($(shell which clang-format-10),)
@sudo apt-get install -y clang-format-10
endif
@extras/scripts/checkstyle.sh
.PHONY: checkstyle-commit
checkstyle-commit:
@ -627,7 +632,7 @@ checkstyle-all: checkstyle-commit checkstyle checkstyle-test
.PHONY: fixstyle
fixstyle:
@build-root/scripts/checkstyle.sh --fix
@extras/scripts/checkstyle.sh --fix
.PHONY: checkstyle-api
checkstyle-api:

@ -1,155 +0,0 @@
#!/bin/bash
# Copyright (c) 2015 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
VPP_DIR=`dirname $0`/../../
EXIT_CODE=0
FIX="0"
FULL="0"
CHECKSTYLED_FILES=""
UNCHECKSTYLED_FILES=""
# If the user provides --fix, then actually fix things
# Note: this is meant for use outside of the CI Jobs, by users cleaning things up
while true; do
case ${1} in
--fix)
FIX="1"
;;
--full)
FULL="1"
;;
esac
shift || break
done
if [ "${FULL}" == "1" ]; then
FILELIST=$(git ls-tree -r HEAD --name-only)
else
FILELIST=$((git diff HEAD~1.. --name-only; git ls-files -m ) | sort -u)
fi
# Check to make sure we have indent. Exit if we don't with an error message, but
# don't *fail*.
command -v indent > /dev/null
if [ $? != 0 ]; then
echo "Cound not find required command \"indent\". Checkstyle aborted"
exit ${EXIT_CODE}
fi
indent --version
# Check to make sure we have clang-format. Exit if we don't with an error message, but
# don't *fail*.
HAVE_CLANG_FORMAT=0
command -v clang-format > /dev/null
if [ $? != 0 ]; then
echo "Could not find command \"clang-format\". Checking C++ files will cause abort"
else
clang-format --version
x=$(echo "" | clang-format 2>&1)
if [[ "$x" == "" ]]; then
HAVE_CLANG_FORMAT=1
else
echo "Output produced while formatting empty file (expected empty string):"
echo "$x"
echo "Could not find working \"clang-format\". Checking C++ files will cause abort"
fi
fi
cd ${VPP_DIR}
git status
for i in ${FILELIST}; do
if [ -f ${i} ] && [ ${i} != "build-root/scripts/checkstyle.sh" ] && [ ${i} != "extras/emacs/fix-coding-style.el" ]; then
grep -q '>>>>>>>' ${i}
if [ $? == 0 ]; then
echo "Unresolved merge conflict detected in" ${i} "... Abort."
exit 1
fi
grep -q "fd.io coding-style-patch-verification: ON" ${i}
if [ $? == 0 ]; then
EXTENSION=`basename ${i} | sed 's/^\w\+.//'`
case ${EXTENSION} in
hpp|cpp|cc|hh)
CMD="clang-format"
if [ ${HAVE_CLANG_FORMAT} == 0 ]; then
echo "C++ file detected. Abort. (missing clang-format)"
exit ${EXIT_CODE}
fi
;;
*)
CMD="indent"
;;
esac
CHECKSTYLED_FILES="${CHECKSTYLED_FILES} ${i}"
if [ ${FIX} == 0 ]; then
if [ "${CMD}" == "clang-format" ]
then
clang-format ${i} > ${i}.out2
else
indent ${i} -o ${i}.out1 > /dev/null 2>&1
indent ${i}.out1 -o ${i}.out2 > /dev/null 2>&1
fi
# Remove trailing whitespace
sed -i -e 's/[[:space:]]*$//' ${i}.out2
diff -q ${i} ${i}.out2
else
if [ "${CMD}" == "clang-format" ]; then
clang-format -i ${i} > /dev/null 2>&1
else
indent ${i}
indent ${i}
fi
# Remove trailing whitespace
sed -i -e 's/[[:space:]]*$//' ${i}
fi
if [ $? != 0 ]; then
EXIT_CODE=1
echo
echo "Checkstyle failed for ${i}."
if [ "${CMD}" == "clang-format" ]; then
echo "Run clang-format as shown to fix the problem:"
echo "clang-format -i ${VPP_DIR}${i}"
else
echo "Run indent (twice!) as shown to fix the problem:"
echo "indent ${VPP_DIR}${i}"
echo "indent ${VPP_DIR}${i}"
fi
fi
if [ -f ${i}.out1 ]; then
rm ${i}.out1
fi
if [ -f ${i}.out2 ]; then
rm ${i}.out2
fi
else
UNCHECKSTYLED_FILES="${UNCHECKSTYLED_FILES} ${i}"
fi
else
UNCHECKSTYLED_FILES="${UNCHECKSTYLED_FILES} ${i}"
fi
done
if [ ${EXIT_CODE} == 0 ]; then
echo "*******************************************************************"
echo "* VPP CHECKSTYLE SUCCESSFULLY COMPLETED"
echo "*******************************************************************"
else
echo "*******************************************************************"
echo "* VPP CHECKSTYLE FAILED"
echo "* CONSULT FAILURE LOG ABOVE"
echo "* NOTE: Running 'build-root/scripts/checkstyle.sh --fix' *MAY* fix the issue"
echo "*******************************************************************"
fi
exit ${EXIT_CODE}

90
extras/scripts/checkstyle.sh Executable file

@ -0,0 +1,90 @@
#!/bin/bash
# Copyright (c) 2020 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -eEo pipefail
CLANG_FORMAT_VER=10
GIT_DIFF_ARGS="-U0 --no-color --relative HEAD~1"
CLANG_FORMAT_DIFF_ARGS="-style file -p1"
SUFFIX="-${CLANG_FORMAT_VER}"
clang-format${SUFFIX} --version
in=$(mktemp)
git diff ${GIT_DIFF_ARGS} > ${in}
line_count=$(sed -n '/^+.*\*INDENT-O[NF][F]\{0,1\}\*/p' ${in} | wc -l)
if [ ${line_count} -gt 0 ] ; then
echo
sed -n '/^+++ /{h}; /^+.*\*INDENT-O[NF][F]\{0,1\}\*/{x;p;x;p;}' ${in}
echo
echo "*******************************************************************"
echo "* CHECKSTYLE FAILED"
echo "* Please remove INDENT-ON and INDENT-OFF from modified lines."
echo "*******************************************************************"
rm ${in}
exit 1
fi
if [ "${1}" == "--fix" ]; then
cat ${in} | clang-format-diff${SUFFIX} ${CLANG_FORMAT_DIFF_ARGS} -i
filelist=$(sed -n 's/^+++ b\/\(.*\.[ch]\)/\1/p' ${in})
git status ${filelist}
rm ${in}
exit 0
fi
line_count=$(sed -n '/^+.*\s\+$/p' ${in} | wc -l)
if [ ${line_count} -gt 0 ] ; then
echo
sed -n '/^+++/h; /^+.*\s\+$/{x;p;x;p;}' ${in}
echo
echo "*******************************************************************"
echo "* CHECKSTYLE FAILED"
echo "* Trailing whitespace detected"
echo "*******************************************************************"
rm ${in}
exit 1
fi
out=$(mktemp)
cat ${in} | clang-format-diff${SUFFIX} ${CLANG_FORMAT_DIFF_ARGS} > ${out}
rm ${in}
line_count=$(cat ${out} | wc -l)
if [ -t 1 ] && [ -n $(tput colors) ] && [ $(tput colors) -ge 1 ] && \
command -v highlight &> /dev/null ; then
highlight --syntax diff -O ansi ${out}
else
cat ${out}
fi
rm ${out}
if [ ${line_count} -gt 0 ] ; then
echo "*******************************************************************"
echo "* CHECKSTYLE FAILED"
echo "* CONSULT DIFF ABOVE"
echo "* NOTE: Running 'extras/scripts/checkstyle.sh --fix' *MAY* fix the issue"
echo "*******************************************************************"
exit 1
else
echo "*******************************************************************"
echo "* CHECKSTYLE SUCCESSFULLY COMPLETED"
echo "*******************************************************************"
exit 0
fi