From 3252f9fb2b92c21bcdb426ca263aed2dce858e53 Mon Sep 17 00:00:00 2001 From: Michael Richters Date: Thu, 24 Mar 2022 21:27:23 -0500 Subject: [PATCH] Switch from astyle to clang-format for automated code formatting I added a `.clang-format` file to try to get as close as possible to our current code formatting. I also updated the makefile targets and the github workflows. Signed-off-by: Michael Richters --- .clang-format | 47 +++++++++++++++++++++++++++++++++++++ .github/workflows/build.yml | 6 ++--- Makefile | 14 ++++------- bin/format-code.sh | 25 ++++++++++++++++++++ 4 files changed, 80 insertions(+), 12 deletions(-) create mode 100644 .clang-format create mode 100755 bin/format-code.sh diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..54e20335 --- /dev/null +++ b/.clang-format @@ -0,0 +1,47 @@ +# -*- mode: yaml -*- +--- +BasedOnStyle: Google +--- +Language: Cpp + +AlignConsecutiveAssignments: Consecutive +## clang-format-15 +# AlignConsecutiveAssignments: +# Enabled: true +# AlignCompound: true +# PadOperators: true +AlignConsecutiveDeclarations: None +AlignConsecutiveMacros: AcrossEmptyLines +AlignEscapedNewlines: Right +AllowShortBlocksOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: WithoutElse +AllowShortFunctionsOnASingleLine: Inline +AllowShortLoopsOnASingleLine: true +AttributeMacros: + - __attribute__((weak)) + - __attribute__((always_inline)) + - __attribute__((noinline)) + - __attribute__((packed)) + - __attribute__((optimize(3))) + - __attribute__((unused)) +BinPackArguments: false +BinPackParameters: false +# BraceWrapping: +# SplitEmptyFunction: false +# SplitEmptyRecord: true +# SplitEmptyNamespace: true +# BreakBeforeBraces: Custom +ColumnLimit: 0 +ConstructorInitializerIndentWidth: 2 +ContinuationIndentWidth: 2 +DerivePointerAlignment: false +FixNamespaceComments: true +IndentCaseLabels: false +KeepEmptyLinesAtTheStartOfBlocks: true +MaxEmptyLinesToKeep: 2 +# PackConstructorInitializers: CurrentLine +PointerAlignment: Right +# ReferenceAlignment: Right +ReflowComments: false +SortIncludes: false +SpaceAfterTemplateKeyword: false diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f1a23b8a..37905e72 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -6,6 +6,7 @@ env: LC_ALL: C KALEIDOSCOPE_CCACHE: 1 ARDUINO_DIRECTORIES_USER: ${{ github.workspace }}/.arduino/user + CLANG_FORMAT_CMD: clang-format-12 jobs: smoke-sketches: runs-on: ubuntu-latest @@ -25,12 +26,11 @@ jobs: - run: sudo apt-get install ccache - run: make setup - run: make -j $(nproc) simulator-tests - check-astyle: + check-formatting: runs-on: ubuntu-latest steps: - - run: sudo apt-get install astyle - uses: actions/checkout@v2 - - run: make check-astyle + - run: make check-formatting check-shellcheck: runs-on: ubuntu-latest steps: diff --git a/Makefile b/Makefile index 2ef20932..c940d834 100644 --- a/Makefile +++ b/Makefile @@ -88,17 +88,13 @@ adjust-git-timestamps: find-filename-conflicts: bin/find-filename-conflicts -.PHONY: astyle test cpplint cpplint-noisy shellcheck smoke-examples find-filename-conflicts prepare-virtual checkout-platform adjust-git-timestamps docker-bash docker-simulator-tests run-tests simulator-tests setup +.PHONY: format check-formatting cpplint cpplint-noisy shellcheck smoke-examples find-filename-conflicts prepare-virtual checkout-platform adjust-git-timestamps docker-bash docker-simulator-tests run-tests simulator-tests setup -astyle: - find ./* -type f \( -name '*.h' -o -name '*.cpp' -o -name '*.ino' \) | grep -v "testing/googletest" | xargs -n 1 astyle --project +format: + bin/format-code.sh -check-astyle: astyle - if ! git diff --exit-code; then \ - >&2 echo "'astyle' found code style differences. Please make astyle and commit your changes"; \ - exit 1; \ - fi; \ - exit 0; +check-formatting: + bin/format-code.sh --check cpplint-noisy: -bin/cpplint.py --filter=-legal/copyright,-build/include,-readability/namespace,-whitespace/line_length,-runtime/references --recursive --extensions=cpp,h,ino src examples diff --git a/bin/format-code.sh b/bin/format-code.sh new file mode 100755 index 00000000..e81dda2e --- /dev/null +++ b/bin/format-code.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +# Allow the caller to specify a particular version of clang-format to use: +: "${CLANG_FORMAT_CMD:=clang-format}" + +# Find all *.cpp and *.h files, except those in `testing/googletest/` and files +# generated by testcase scripts, and run `clang-format` on them: +find ./* -type f \( -name '*.h' -o -name '*.cpp' \) \ + -not \( -path './testing/googletest/*' -o -name 'generated-testcase.cpp' \) \ + -print0 | \ + xargs -0 "${CLANG_FORMAT_CMD}" -i + +# If we get the `--check` option, return an error if there are any changes to +# the git working tree after running `clang-format`: +if [[ $1 == '--check' ]]; then + + if ! git diff --quiet; then + cat >&2 <