From b61d54652477503ef2b058fd8304e6c16cd5cef8 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Tue, 15 Dec 2020 10:36:15 -0500 Subject: [PATCH] Add `tools/format-cpp` script The new `format-cpp` script uses `clang-format -i` to inline-format all the C++ files except `src/cpp-common/optional/optional.hpp` and `src/cpp-common/string_view/string_view.hpp`. You can run it from any directory: it always runs `clang-format -i` for the whole project. By default, the script runs `clang-format -i`; you can override this with the `FORMATTER` environment variable, for example: $ FORMATTER='clang-format-10 -i' ./tools/format-cpp Signed-off-by: Philippe Proulx Change-Id: I1049eabf8da71ce5c1d8443e1e2cdc9a73f4b4d5 Reviewed-on: https://review.lttng.org/c/babeltrace/+/4592 Reviewed-by: Simon Marchi --- CONTRIBUTING.adoc | 9 ++++++++- Makefile.am | 1 + tools/format-cpp | 12 ++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100755 tools/format-cpp diff --git a/CONTRIBUTING.adoc b/CONTRIBUTING.adoc index b9f26eb1..7052f2e8 100644 --- a/CONTRIBUTING.adoc +++ b/CONTRIBUTING.adoc @@ -1689,7 +1689,14 @@ file. To automatically format all the project's {cpp} files, run: ---- -$ find -iname '*.cpp' -o -iname '*.hpp' -exec clang-format -i '{}' ';' +$ ./tools/format-cpp +---- + +Use the `FORMATTER` environment variable to override the default +formatter (`clang-format{nbsp}-i`): + +---- +$ FORMATTER='clang-format-10 -i' ./tools/format-cpp ---- ==== Naming diff --git a/Makefile.am b/Makefile.am index 90007da2..ec8ba78b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -25,4 +25,5 @@ EXTRA_DIST = \ LICENSES/LGPL-2.1 \ LICENSES/MIT \ std-ext-lib.txt \ + tools/format-cpp \ version diff --git a/tools/format-cpp b/tools/format-cpp new file mode 100755 index 00000000..48703c29 --- /dev/null +++ b/tools/format-cpp @@ -0,0 +1,12 @@ +#!/bin/bash +# +# SPDX-License-Identifier: GPL-2.0-only +# +# Copyright (C) 2020 Philippe Proulx + +FORMATTER="${FORMATTER:-clang-format -i}" +root_dir="$(dirname "${BASH_SOURCE[0]}")/.." +find "$root_dir" \( -name '*.cpp' -o -name '*.hpp' \) \ + ! -wholename '*/cpp-common/optional.hpp' \ + ! -wholename '*/cpp-common/string_view.hpp' \ + -exec $FORMATTER '{}' ';' -- 2.34.1