From 21934a7f086082b81dd5c4541dead6f16b7322dd Mon Sep 17 00:00:00 2001 From: Alain Carlucci Date: Wed, 3 Nov 2021 20:59:15 +0100 Subject: [PATCH] Add .clang-format and auto-indent script --- .clang-format | 34 +++++++++++++++++++++++++++ scripts/clang_format.sh | 52 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 .clang-format create mode 100755 scripts/clang_format.sh diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..62b427f8 --- /dev/null +++ b/.clang-format @@ -0,0 +1,34 @@ +--- +BasedOnStyle: Google +AlignConsecutiveBitFields: AcrossEmptyLinesAndComments +AlignTrailingComments: 'true' +AlignConsecutiveAssignments: Consecutive +AllowShortFunctionsOnASingleLine: 'false' +ColumnLimit: '80' +DerivePointerAlignment: 'false' +IndentExternBlock: NoIndent +IndentWidth: '4' +NamespaceIndentation: Inner + +BreakBeforeBraces: Custom +BraceWrapping: + AfterCaseLabel: 'true' + AfterClass: 'true' + AfterControlStatement: Always + AfterEnum: 'true' + AfterFunction: 'true' + AfterNamespace: 'true' + AfterObjCDeclaration: 'true' + AfterStruct: 'true' + AfterUnion: 'true' + AfterExternBlock: 'false' + BeforeCatch: 'true' + BeforeElse: 'true' + BeforeLambdaBody: 'true' + BeforeWhile: 'false' + IndentBraces: 'false' + SplitEmptyFunction: 'true' + SplitEmptyRecord: 'true' + SplitEmptyNamespace: 'true' + +... diff --git a/scripts/clang_format.sh b/scripts/clang_format.sh new file mode 100755 index 00000000..43e0d5e1 --- /dev/null +++ b/scripts/clang_format.sh @@ -0,0 +1,52 @@ +#!/bin/bash -e + +# Copyright (C) 2021 by Alain Carlucci. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see + +function show_help { + echo "OpenRTX clang-format tool." + echo "" + echo "Usage: $0 [--check] [--help]" + echo "" + echo "To apply clang-format style to the whole codebase, run without arguments" + echo "To check if the codebase is compliant, run with --check" +} + +if [ $# -gt 1 ]; then + show_help + exit 1 +fi + +if [ $# -eq 1 ]; then + if [ "$1" == "--help" ]; then + show_help + exit 0 + fi + + if [ "$1" != "--check" ]; then + echo "Invalid argument $1" + show_help + exit 1 + fi +fi + +FILE_LIST=$(git ls-files | egrep '\.(c|cpp|h)$' | egrep -v 'lib/|subprojects/|platform/mcu') + +CHECK_ARGS="" +if [ "$1" == "--check" ]; then + CHECK_ARGS="--dry-run -Werror" +fi + +clang-format $CHECK_ARGS -i $FILE_LIST