From 6adb70470a94c33e838a1bcda875dc296eeb4905 Mon Sep 17 00:00:00 2001 From: Florian Schmaus <flow@cs.fau.de> Date: Wed, 10 Nov 2021 11:20:28 +0100 Subject: [PATCH] [tools] Update check-format (from Mazstab) Sync tools/check-format of EMPER and Mazstab by using the newer Mazstab version of the script. --- tools/check-format | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/tools/check-format b/tools/check-format index 005c77c1..f3a9b43f 100755 --- a/tools/check-format +++ b/tools/check-format @@ -15,11 +15,16 @@ SCRIPTDIR="`pwd`"; popd > /dev/null DEBUG=false -while getopts d OPT; do +VERBOSE=false +while getopts dv OPT; do case $OPT in d) set -x DEBUG=true + VERBOSE=true + ;; + v) + VERBOSE=true ;; *) echo "usage: ${0##*/} [-dq} [--] ARGS..." @@ -33,19 +38,42 @@ ROOTDIR=$(readlink -f "${SCRIPTDIR}/..") MAX_PROCS=$(nproc) -CHECKED_FILES_FILE=$(mktemp) +FILES_TO_CHECK_FILE=$(mktemp) +cleanup() { + rm "${FILES_TO_CHECK_FILE}" +} if ! $DEBUG; then - trap 'rm "${CHECKED_FILES_FILE}"' EXIT + trap cleanup EXIT fi cd "${ROOTDIR}" + +PRUNE_PATHS=() +PRUNE_PATHS+=(./build*) # Generated files +PRUNE_PATHS+=(./subprojects) # Subprojects, since are under different licenses + +PRUNE_PATH_ARG="" +# https://stackoverflow.com/a/12298615/194894 +for path in "${PRUNE_PATHS[@]::${#PRUNE_PATHS[@]}-1}"; do + PRUNE_PATH_ARG+="-path ${path} -o " +done +PRUNE_PATH_ARG+="-path ${PRUNE_PATHS[-1]}" + +# shellcheck disable=SC2086 +find . \( ${PRUNE_PATH_ARG} \) -prune -o \ + -type f -regextype posix-extended -regex '.*\.(c|h|cpp|hpp)' -print0 \ + > "${FILES_TO_CHECK_FILE}" + +if $VERBOSE; then + echo "About to check the following files for correct formatting via clang-format" + tr '\0' '\n' < "${FILES_TO_CHECK_FILE}" +fi + # Note that the --dry-run and --Werror clang-format arguments require # clang-format 10 or higher. See https://reviews.llvm.org/D68554 -find . \( -path '*/\.*' -o -path "./subprojects*" -o -path "./build*" \) -prune -o \ - -type f -regextype posix-extended -regex '.*\.(c|h|cpp|hpp)' -print0 |\ - tee "${CHECKED_FILES_FILE}" |\ - xargs --null --max-args=3 --max-procs="${MAX_PROCS}" \ - clang-format --style=file --dry-run -Werror +xargs --null --max-args=3 --max-procs="${MAX_PROCS}" \ + clang-format --style=file --dry-run -Werror \ + < "${FILES_TO_CHECK_FILE}" -FILE_COUNT=$(<"${CHECKED_FILES_FILE}" tr -cd '\0' | wc -c) +FILE_COUNT=$(<"${FILES_TO_CHECK_FILE}" tr -cd '\0' | wc -c) echo "Checked ${FILE_COUNT} files for format violations" -- GitLab