tools/format-cpp: make shellcheck happy again
[babeltrace.git] / tools / format-cpp
1 #!/bin/bash
2 #
3 # SPDX-License-Identifier: GPL-2.0-only
4 #
5 # Copyright (C) 2020-2022 Philippe Proulx <pproulx@efficios.com>
6
7 expected_formatter_major_version=15
8
9 # Runs the formatter, making sure it's the expected version.
10 format_cpp() {
11 local formatter=("$@")
12 local version
13
14 if ! version=$("${formatter[@]}" --version); then
15 echo "Cannot execute \`${formatter[*]} --version\`." >&2
16 return 1
17 fi
18
19 if [[ "$version" != *"clang-format version $expected_formatter_major_version"* ]]; then
20 echo "Expecting clang-format $expected_formatter_major_version." >&2
21 echo >&2
22 echo Got: >&2
23 echo >&2
24 echo "$version" >&2
25 return 1
26 fi
27
28 local root_dir
29
30 root_dir="$(dirname "${BASH_SOURCE[0]}")/.."
31
32 # Using xargs to fail as soon as the formatter fails (`-exec`
33 # won't stop if its subprocess fails).
34 find "$root_dir" \( -name '*.cpp' -o -name '*.hpp' \) \
35 ! -wholename '*/cpp-common/optional.hpp' \
36 ! -wholename '*/cpp-common/string_view.hpp' \
37 ! -wholename '*/cpp-common/nlohmann/json.hpp' \
38 -print0 | xargs -P"$(nproc)" -n1 -0 "${formatter[@]}"
39 }
40
41 if [[ -n "$FORMATTER" ]]; then
42 # Try using environment-provided formatter
43 read -ra formatter <<< "$FORMATTER"
44 elif command -v clang-format-$expected_formatter_major_version &> /dev/null; then
45 # Try using the expected version of clang-format
46 formatter=("clang-format-$expected_formatter_major_version" -i)
47 else
48 # Try using `clang-format` as is
49 formatter=(clang-format -i)
50 fi
51
52 # Try to format files
53 format_cpp "${formatter[@]}"
This page took 0.030095 seconds and 4 git commands to generate.