summaryrefslogtreecommitdiffstats
path: root/tools/clang-format.sh
diff options
context:
space:
mode:
authorAndrei Pavel <andrei@isc.org>2021-01-22 16:19:51 +0100
committerAndrei Pavel <andrei@isc.org>2021-01-22 16:19:51 +0100
commitc7311fa52bf9eb65e1c81c01377f4922139beef4 (patch)
treee57c563c56ff83f10d22365fb15cf75abd2c803a /tools/clang-format.sh
parent[#1527] added support for fedora 33 and ubunut 20.10 (diff)
downloadkea-c7311fa52bf9eb65e1c81c01377f4922139beef4.tar.xz
kea-c7311fa52bf9eb65e1c81c01377f4922139beef4.zip
[#1455] clang-format, uncrustify
Diffstat (limited to 'tools/clang-format.sh')
-rwxr-xr-xtools/clang-format.sh105
1 files changed, 105 insertions, 0 deletions
diff --git a/tools/clang-format.sh b/tools/clang-format.sh
new file mode 100755
index 0000000000..f039d8c8b7
--- /dev/null
+++ b/tools/clang-format.sh
@@ -0,0 +1,105 @@
+#!/bin/sh
+
+# Copyright (C) 2020 Internet Systems Consortium, Inc. ("ISC")
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+# Usage:
+#
+# ./tools/clang-format.sh [-d|--debug] [-h|--help] [$directory|$file ...]
+#
+# Run from the root of the repository to format all C++ files under specified
+# directories or specified files or current directory in case of no arguments.
+
+#------------------------------------------------------------------------------#
+
+set -eu
+
+# Customizations
+extensions_regex='(\.cpp|\.cc|\.C|\.cxx|\.m|\.hpp|\.hh|\.h|\.H|\.hxx|\.tpp)$'
+
+# Print usage.
+print_usage() {
+ printf \
+'Usage: %s {{options}}
+Options:
+ [-d|--debug] enable debug mode, showing every executed command
+ [-h|--help] print usage (this text)
+' \
+ "$(basename "${0}")"
+}
+
+# Parse parameters.
+while test ${#} -gt 0; do
+ case "${1}" in
+ # [-d|--debug] enable debug mode, showing every executed command
+ '-d'|'--debug') set -vx ;;
+
+ # [-h|--help] print usage (this text).
+ '-h'|'--help') print_usage ;;
+
+ # Allow extra arguments, they should be directories or files to be formatted.
+ *) break ;;
+ esac; shift
+done
+
+# Get script path.
+script_path=$(cd "$(dirname "${0}")" && pwd)
+
+# Use current directory when called without an argument.
+if test ${#} = 0; then
+ set -- .
+fi
+
+# Generated files will be filtered out.
+filtered_out=$("${script_path}/print-generated-files.sh")
+
+# For all arguments...
+parameters=
+while test ${#} -gt 0; do
+ # Preserve parameters that begin with dash and pass them to clang-format.
+ if test "$(printf '%s' "${1}" | cut -c 1)" = '-'; then
+ parameters="${parameters} ${1}"
+ shift
+ continue
+ fi
+
+ # The rest of the parameters are considered files or directories.
+ file=${1}
+
+ # Get absolute path.
+ if test "$(printf '%s' "${file}" | grep -Eo '^.')" != '/'; then
+ basename=$(basename "${file}")
+ if test "${basename}" = .; then
+ basename=
+ fi
+ file="$(cd "$(dirname "${file}")" && pwd)/${basename}"
+ fi
+ printf '%s\n' "${file}"
+
+ if test -f "${file}"; then
+ # Format file.
+ # shellcheck disable=SC2046
+ # We specifically want word splitting for the parameters.
+ clang-format --style=file -i $(printf '%s' "${parameters}") "${file}"
+ elif test -d "${file}"; then
+ # Get list of files to format.
+ cd "$(git rev-parse --show-toplevel)"
+ files=$(git ls-files | xargs -n1 printf "${PWD}/%s\\n" | grep -F "${file}" \
+ | grep -E "${extensions_regex}")
+
+ # Filter out generated files.
+ for file in ${filtered_out}; do
+ files=$(printf '%s\n' "${files}" | grep -Fv "${file}" | sed '/^$/d')
+ done
+
+ # For all files...
+ for i in ${files}; do
+ "${0}" "${i}"
+ done
+ fi
+
+ shift
+done