Use compiler-agnostic defines to silence warning
[lttng-tools.git] / format-cpp
... / ...
CommitLineData
1#!/bin/bash
2#
3# SPDX-License-Identifier: GPL-2.0-only
4#
5# Copyright (C) 2020-2022 Philippe Proulx <pproulx@efficios.com>
6
7expected_formatter_major_version=14
8
9SCRIPT_DIR=$(dirname "$0")/
10
11# Runs the formatter, making sure it's the expected version.
12format_cpp() {
13 local formatter=$1
14 local version
15
16 version=$($formatter --version)
17
18 # shellcheck disable=SC2181
19 if (($? != 0)); then
20 echo "Cannot execute \`$formatter --version\`." >&2
21 return 1
22 fi
23
24 if [[ "$version" != *"clang-format version $expected_formatter_major_version"* ]]; then
25 echo "Expecting clang-format $expected_formatter_major_version." >&2
26 echo -n Got: >&2
27 echo " \`$version\`" >&2
28 echo >&2
29 echo "Use the FORMATTER environment variable to specify the location of clang-format $expected_formatter_major_version"
30 return 1
31 fi
32
33 local root_dir
34
35 root_dir="$(dirname "${BASH_SOURCE[0]}")"
36
37 # Using xargs to fail as soon as the formatter fails (`-exec`
38 # won't stop if its subprocess fails).
39 #
40 # Since clang-format 14 does not support ignore files, their
41 # support is crudely emulated here.
42 #
43 # shellcheck disable=SC2086
44 find "$root_dir" -path './src/vendor' -prune \
45 -o -type f \( -name '*\.h' -o -name '*\.hpp' -o -name '*\.c' -o -name '*\.cpp' \) \
46 -not -path '*/\.*' -print0 | grep -zv -f "$SCRIPT_DIR"/.clang-format-ignore | \
47 xargs -P"$(nproc)" -n1 -0 $formatter -i --style=file --fallback-style=none
48}
49
50if [[ -n "$FORMATTER" ]]; then
51 # Try using environment-provided formatter
52 formatter=$FORMATTER
53elif command -v clang-format-$expected_formatter_major_version &> /dev/null; then
54 # Try using the expected version of clang-format
55 formatter="clang-format-$expected_formatter_major_version"
56else
57 # Try using `clang-format` as is
58 formatter='clang-format'
59fi
60
61# Try to format files
62format_cpp "$formatter"
This page took 0.029329 seconds and 4 git commands to generate.