Verified Commit 27f06a25 authored by Marc Vef's avatar Marc Vef
Browse files

Adding clang-format binary path arg to check_format.sh

parent 4d2e18c0
Loading
Loading
Loading
Loading
+29 −3
Original line number Diff line number Diff line
@@ -3,10 +3,11 @@
PROJECT_SRC="$(pwd)/src"
PROJECT_INCLUDE="$(pwd)/include"
RUN_FORMAT=false
CLANG_FORMAT_BIN=""

usage_short() {
    echo "
usage: check_format.sh [-h] [-s <PROJECT_SRC>] [-i <PROJECT_INCLUDE>]
usage: check_format.sh [-h] [-s <PROJECT_SRC>] [-i <PROJECT_INCLUDE>] [-c <CLANG_FORMAT_BIN>] [--run_format]
	"
}

@@ -24,6 +25,9 @@ optional arguments:
    -i <PROJECT_INCLUDE>, --include <PROJECT_INCLUDE>
                        path to the include/ directory of the project
                        (default: 'pwd/include')
    -c <CLANG_FORMAT_BIN>, --clang_format_path <CLANG_FORMAT_BIN>
                        path to clang-format binary
                        (default: looks for 'clang-format' or 'clang-format-10')
    -r, --run_format    run clang-formatter before formatting check
                        DISCLAIMER: FILES ARE MODIFIED IN PLACE!
"
@@ -44,6 +48,15 @@ while [[ $# -gt 0 ]]; do
        shift # past argument
        shift # past value
        ;;
    -c | --clang_format_path)
        CLANG_FORMAT_BIN="$(readlink -mn "${2}")"
        if [[ ! -x $CLANG_FORMAT_BIN ]]; then
            echo "*** ERR: clang-format path $CLANG_FORMAT_BIN is not an executable! Exiting ..."
            exit 1
        fi
        shift # past argument
        shift # past value
        ;;
    -r | --run_format)
        RUN_FORMAT=true
        shift # past argument
@@ -60,6 +73,19 @@ while [[ $# -gt 0 ]]; do
done
set -- "${POSITIONAL[@]}" # restore positional parameters

# check for clang-format executable if it hasn't been set by the user
if [[ -z $CLANG_FORMAT_BIN ]]; then
    CLANG_FORMAT_BIN=$(command -v clang-format)
    if [[ -z $CLANG_FORMAT_BIN ]]; then
        CLANG_FORMAT_BIN=$(command -v clang-format-10)
        # if it still doesn't exist exit
        if [[ -z $CLANG_FORMAT_BIN ]]; then
            echo "*** ERR: clang-format not found! Exiting ..."
            exit 1
        fi
    fi
fi

FAIL=false

echo "* Source directory: $PROJECT_SRC"
@@ -81,11 +107,11 @@ else
fi
echo "------------------------------------------"
while IFS= read -r -d '' FILE; do
    UNFORMATTED_LINES=$(diff -u <(cat "$FILE") <(clang-format -style=file "$FILE") | wc -l)
    UNFORMATTED_LINES=$(diff -u <(cat "$FILE") <($CLANG_FORMAT_BIN -style=file "$FILE") | wc -l)
    if ((UNFORMATTED_LINES > 0)); then
        if [[ "$RUN_FORMAT" == true ]]; then
            echo "*** Reformatting $UNFORMATTED_LINES lines in '$FILE'"
            clang-format -i -style=file "$FILE"
            $CLANG_FORMAT_BIN -i -style=file "$FILE"
        else
            echo -n "$FILE "
            echo "$UNFORMATTED_LINES"