vscode: Add configurations to run the executables under the debugger
[lttng-tools.git] / format-cpp
CommitLineData
e89902fd
JG
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
9# Runs the formatter, making sure it's the expected version.
10format_cpp() {
11 local formatter=$1
12 local version
13
14 version=$($formatter --version)
15
16 # shellcheck disable=SC2181
17 if (($? != 0)); then
18 echo "Cannot execute \`$formatter --version\`." >&2
19 return 1
20 fi
21
22 if [[ "$version" != *"clang-format version $expected_formatter_major_version"* ]]; then
23 echo "Expecting clang-format $expected_formatter_major_version." >&2
24 echo -n Got: >&2
25 echo " \`$version\`" >&2
26 echo >&2
27 echo "Use the FORMATTER environment variable to specify the location of clang-format $expected_formatter_major_version"
28 return 1
29 fi
30
31 local root_dir
32
33 root_dir="$(dirname "${BASH_SOURCE[0]}")"
34
35 # Using xargs to fail as soon as the formatter fails (`-exec`
36 # won't stop if its subprocess fails).
37 #
38 # shellcheck disable=SC2086
39 find "$root_dir" -path './src/vendor' -prune \
40 -o -type f \( -name '*\.h' -o -name '*\.hpp' -o -name '*\.c' -o -name '*\.cpp' \) \
04938fae 41 -not -path '*/\.*' -print0 | xargs -P$(nproc) -n1 -0 $formatter -i --style=file --fallback-style=none
e89902fd
JG
42}
43
44if [[ -n "$FORMATTER" ]]; then
45 # Try using environment-provided formatter
46 formatter=$FORMATTER
47elif command -v clang-format-$expected_formatter_major_version &> /dev/null; then
48 # Try using the expected version of clang-format
78de3202 49 formatter="clang-format-$expected_formatter_major_version"
e89902fd
JG
50else
51 # Try using `clang-format` as is
78de3202 52 formatter='clang-format'
e89902fd
JG
53fi
54
55# Try to format files
56format_cpp "$formatter"
This page took 0.029376 seconds and 4 git commands to generate.