From 85510893d5b70c494679c675acc5e4a36181097a Mon Sep 17 00:00:00 2001 From: Andreas Traczyk Date: Tue, 4 Aug 2020 11:41:29 -0400 Subject: [PATCH] misc: add clang format stylesheet - Run to clang-format all committed files. - Run with --all to format all cpp|cxx|cc|h|hpp|mm files in src/. - Run with --install to install as a pre-commit hook. Requires that clang-format be installed. Devs should either install the pre-commit hook or configure their editors to format on save etc. Change-Id: I73c2b19e0673115c1f7d33cbae5094f3a88d43e3 --- .clang-format | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++ astylerc | 18 --------- format.sh | 91 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 201 insertions(+), 18 deletions(-) create mode 100644 .clang-format delete mode 100644 astylerc create mode 100755 format.sh diff --git a/.clang-format b/.clang-format new file mode 100644 index 000000000..3b08cfb96 --- /dev/null +++ b/.clang-format @@ -0,0 +1,110 @@ +# Based on the .clang-format for Qt Creator +# +# This is for clang-format >= 10.0. +# +# https://releases.llvm.org/10.0.0/tools/clang/docs/ClangFormatStyleOptions.html +# +--- +Language: Cpp +AccessModifierOffset: -4 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: true +AlignConsecutiveDeclarations: false +AlignConsecutiveMacros: true +AlignEscapedNewlines: DontAlign +AlignOperands: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Inline +AllowShortIfStatementsOnASingleLine: false +AllowShortLambdasOnASingleLine: Inline +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterReturnType: TopLevelDefinitions +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: Yes +BinPackArguments: false +BinPackParameters: false +BraceWrapping: + AfterClass: true + AfterControlStatement: false + AfterEnum: false + AfterFunction: true + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: true + AfterUnion: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyRecord: false + SplitEmptyNamespace: false +BreakBeforeBinaryOperators: All +BreakBeforeBraces: Custom +BreakBeforeInheritanceComma: false +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeComma +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 100 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - forever # avoids { wrapped to next line + - foreach + - Q_FOREACH + - BOOST_FOREACH +IncludeCategories: + - Regex: '^ -# Savoir-faire Linux Inc -# https://jami.net - -style=stroustrup # stroustrup style http://astyle.sourceforge.net/astyle.html#_style=stroustrup -indent=spaces=4 # Use spaces instead of tabs for indentation -indent-classes # Indent 'class' and 'struct' blocks so that the blocks 'public:', 'protected:' and 'private:' are indented -indent-switches # Indent 'switch' blocks so that the 'case X:' statements are indented in the switch block -break-blocks # Pad empty lines around header blocks (e.g. 'if', 'while'...). -brackets=linux -unpad-paren # Remove unwanted space around parentheses -pad-header # Insert space padding after paren headers only (e.g. 'if', 'for', 'while'...) -pad-oper # Insert space padding around operator -formatted # only display files that have changed -suffix=none # don't create backup files (that's what version control is for) diff --git a/format.sh b/format.sh new file mode 100755 index 000000000..f1c994ab3 --- /dev/null +++ b/format.sh @@ -0,0 +1,91 @@ +#!/usr/bin/env bash + +set -e + +command_exists () +{ + type "$1" &> /dev/null ; +} + +CFVERSION="9" +CLANGFORMAT="" +if command_exists clang-format; then + CLANGFORMAT=clang-format +else + if command_exists clang-format-${CFVERSION}; then + CLANGFORMAT=clang-format-${CFVERSION} + fi +fi + +if ! command -v $CLANGFORMAT &> /dev/null; then + echo "Required version of clang-format not found" + exit 1 +fi + +format_file() +{ + if [ -f "${1}" ]; then + $CLANGFORMAT -i -style=file "${1}" + fi +} + +format_files() +{ + for file in $1; do + echo -ne "Formatting: ${file}\\033[0K\\r" + format_file "${file}" + done +} + +exit_if_no_files() +{ + echo No files to format + exit 0 +} + +install_hook() +{ + # check for lone repo ring-daemon + hooks_path=".git/hooks" + if [ ! -d "$hooks_path=" ]; then + # or ring-project + hooks_path="../.git/modules/daemon/hooks" + if [ ! -d "$hooks_path" ]; then + echo "Can't find a git directory" + exit 1 + fi + fi + echo Installing pre-commit hook in "$hooks_path" + echo "/bin/bash $0" > "$hooks_path"/pre-commit + chmod +x "$hooks_path"/pre-commit +} + +display_help() +{ + echo "Usage: $0 [OPTION...] -- Clang format source files with a .clang-format file" >&2 + echo + echo " --all format all files instead of only committed ones" + echo " --install install a pre-commit hook to run this script" + echo +} + +if [ "$1" == "--help" ]; then + display_help + exit 0 +fi + +case "${1}" in + --all ) + files=$(find src -regex '.*\.\(cpp\|hpp\|cc\|cxx\|h\|mm\)') || true + echo Formatting all source files... + format_files "$files" + ;; + --install ) + install_hook + ;; + * ) + files=$(git diff-index --cached --name-only HEAD | grep -iE '\.(cpp|cxx|cc|h|hpp|mm)') || exit_if_no_files + echo Formatting committed source files... + format_files "$files" + ;; +esac