From 6604df0795529c7d54b43dd669b9b7500e614889 Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Wed, 10 Jan 2018 10:54:24 +0100 Subject: [PATCH] Added clang format script. new code formatting rules are now enforced by this script. --- .clang-format | 125 +++++++++++++++++++++++++++++++++++++++ .github/PULL_REQUEST_TEMPLATE.md | 3 +- CMakeLists.txt | 4 ++ cmake/ClangFormat.cmake | 48 +++++++++++++++ scripts/autoformat.sh | 10 ---- scripts/format_code.sh | 42 ------------- 6 files changed, 179 insertions(+), 53 deletions(-) create mode 100644 .clang-format create mode 100644 cmake/ClangFormat.cmake delete mode 100755 scripts/autoformat.sh delete mode 100755 scripts/format_code.sh diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..a15ddf8 --- /dev/null +++ b/.clang-format @@ -0,0 +1,125 @@ +--- +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Left +AlignOperands: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: false +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterClass: true + AfterControlStatement: true + AfterEnum: true + AfterFunction: true + AfterNamespace: true + AfterObjCDeclaration: true + AfterStruct: true + AfterUnion: true + AfterExternBlock: true + BeforeCatch: true + BeforeElse: true + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Allman +BreakBeforeInheritanceComma: false +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakStringLiterals: true +ColumnLimit: 100 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: false +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: false +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + - Regex: '.*' + Priority: 1 +IncludeIsMainRegex: '(Test)?$' +IndentCaseLabels: false +IndentPPDirectives: AfterHash +IndentWidth: 4 +IndentWrappedFunctionNames: false +KeepEmptyLinesAtTheStartOfBlocks: true +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Left +ReflowComments: true +SortIncludes: false +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: false +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +TabWidth: 4 +UseTab: ForIndentation +... +Language: Cpp +Standard: Auto +NamespaceIndentation: All +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +... +Language: ObjC +PointerBindsToType: false +ObjCSpaceAfterProperty: true +SortIncludes: false +ObjCBlockIndentWidth: 4 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +... +Language: Java +BreakAfterJavaFieldAnnotations: false +... +Language: JavaScript +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +... +Language: Proto +... +Language: TableGen +... +Language: TextProto +... diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index f76bc12..91a1cef 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -5,7 +5,8 @@ ## Preparations before creating a pull * Rebase your branch to current master, no merges allowed! * Try to clean up your commit history, group changes to commits -* Check your formatting! A _astyle_ script can be found at ```./scripts/format_code.sh``` +* Check your formatting! A _clang-format_ script can be found at ```.clang-format``` + * The cmake target ```clangformat``` reformats the whole codebase * Optional (but higly recommended) * Run a clang scanbuild before and after your changes to avoid introducing new bugs * Run your compiler at pedantic level to check for new warnings diff --git a/CMakeLists.txt b/CMakeLists.txt index fd36e0b..6022c5e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,6 +51,10 @@ endif() include(CheckCmakeCompat) # Include cmake modules +if(WITH_CLANG_FORMAT) + include(ClangFormat) +endif() + include(CheckIncludeFiles) include(CheckLibraryExists) include(CheckSymbolExists) diff --git a/cmake/ClangFormat.cmake b/cmake/ClangFormat.cmake new file mode 100644 index 0000000..8985ef3 --- /dev/null +++ b/cmake/ClangFormat.cmake @@ -0,0 +1,48 @@ +# get all project files +file(GLOB_RECURSE ALL_SOURCE_FILES *.cpp *.c *.h *.m *.java) +# minimum version required +set(_CLANG_FORMAT_MINIMUM_VERSION 7.0.0) + +find_program(CLANG_FORMAT + NAMES + clang-format-8 + clang-format-7 + clang-format + ) + +if (NOT CLANG_FORMAT) + message(WARNING "clang-format not found in path! code format target not available.") +else() + execute_process( + COMMAND ${CLANG_FORMAT} "--version" + OUTPUT_VARIABLE _CLANG_FORMAT_VERSION + RESULT_VARIABLE _CLANG_FORMAT_VERSION_FAILED + ) + + if (_CLANG_FORMAT_VERSION_FAILED) + message(WARNING "A problem was encounterd with ${CLANG_FORMAT}") + return() + endif() + + string(REGEX MATCH "([7-9]|[1-9][0-9])\\.[0-9]\\.[0-9]" CLANG_FORMAT_VERSION + "${_CLANG_FORMAT_VERSION}") + + if (NOT CLANG_FORMAT_VERSION) + message(WARNING "problem parsing clang-fromat version for ${CLANG_FORMAT}") + return() + endif() + + if (${CLANG_FORMAT_VERSION} VERSION_LESS ${_CLANG_FORMAT_MINIMUM_VERSION}) + message(WARNING "clang-format version ${CLANG_FORMAT_VERSION} not supported") + message(WARNING "Minimum version required: ${_CLANG_FORMAT_MINIMUM_VERSION}") + return() + endif() + + add_custom_target( + clangformat + COMMAND ${CLANG_FORMAT} + -style=file + -i + ${ALL_SOURCE_FILES} + ) +endif() diff --git a/scripts/autoformat.sh b/scripts/autoformat.sh deleted file mode 100755 index f93733f..0000000 --- a/scripts/autoformat.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash -MY_PATH="`dirname \"$0\"`" # relative -MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized -CHANGESET=`git status | cut -d ':' -f 2 | grep -vE "#|no" | grep -E "*\.h|*\.c"` # get filenames from git status - -for f in $CHANGESET; do - if [ -e $f ]; then - sh $MY_PATH/format_code.sh $f - fi -done diff --git a/scripts/format_code.sh b/scripts/format_code.sh deleted file mode 100755 index f3561ba..0000000 --- a/scripts/format_code.sh +++ /dev/null @@ -1,42 +0,0 @@ -#!/bin/bash - -ASTYLE=$(which astyle) - -if [ ! -x "$ASTYLE" ]; then - echo "No astyle found in path, please install." - exit 1 -fi - -# Need at least astyle 2.03 due to bugs in older versions -# indenting headers with extern "C" -STR_VERSION=$($ASTYLE --version 2>&1) -VERSION=$(echo $STR_VERSION | cut -d ' ' -f4) -MAJOR_VERSION=$(echo $VERSION | cut -d'.' -f1) -MINOR_VERSION=$(echo $VERSION | cut -d'.' -f2) - -if [ "$MAJOR_VERSION" -lt "2" ]; -then - echo "Your version of astyle($VERSION) is too old, need at least 2.03" - exit 1 -elif [ "$MAJOR_VERSION" -eq "2" ]; -then - if [ "$MINOR_VERSION" -lt "3" ]; - then - echo "Your version of astyle($VERSION) is too old, need at least 2.03" - exit 1 - fi -fi - -if [ $# -le 0 ]; then - echo "Usage:" - echo -e "\t$0 [ ...]" - exit 2 -fi - -$ASTYLE --lineend=linux --mode=c --indent=tab=4 --pad-header --pad-oper --style=allman --min-conditional-indent=0 \ - --indent-switches --indent-cases --indent-preprocessor -k1 --max-code-length=100 \ - --indent-col1-comments --delete-empty-lines --break-closing-brackets \ - --align-pointer=type --indent-labels -xe --break-after-logical \ - --unpad-paren --break-blocks $@ - -exit $? -- 2.7.4