diff options
-rw-r--r-- | .clang-format | 34 | ||||
-rwxr-xr-x | scripts/clang-format.sh | 49 |
2 files changed, 83 insertions, 0 deletions
diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000000..df1588d8de --- /dev/null +++ b/.clang-format @@ -0,0 +1,34 @@ +BasedOnStyle: LLVM +AccessModifierOffset: -4 +AllowShortFunctionsOnASingleLine: None +AlwaysBreakTemplateDeclarations: true +BinPackParameters: false +BraceWrapping: + AfterClass: true + AfterControlStatement: true + AfterEnum: true + AfterFunction: true + AfterNamespace: true + AfterObjCDeclaration: true + AfterStruct: true + AfterUnion: true + AfterExternBlock: false + BeforeCatch: true + BeforeElse: true + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyRecord: false + SplitEmptyNamespace: true +BreakBeforeBraces: Custom +BreakConstructorInitializersBeforeComma: true +BreakConstructorInitializers: BeforeColon +Cpp11BracedListStyle: false +IndentCaseLabels: true +IndentWidth: 4 +IndentWrappedFunctionNames: true +PointerAlignment: Left +SpacesInContainerLiterals: false +AlignConsecutiveAssignments: true +ColumnLimit: 120 +ReflowComments: false +SpacesBeforeTrailingComments: 4 diff --git a/scripts/clang-format.sh b/scripts/clang-format.sh new file mode 100755 index 0000000000..5d203ad161 --- /dev/null +++ b/scripts/clang-format.sh @@ -0,0 +1,49 @@ +#!/bin/bash +# +# Copyright © 2021-2023 Arm Ltd. All rights reserved. +# SPDX-License-Identifier: MIT +# + +# +# Usage: clang-format.sh [path to file or directory] +# +# Formats all git-tracked files in the repository according to clang-format rules. +# +# Takes an optional parameter to limit the scope to a specific directory or file. +# +# The clang-format rules can be found in the top level directory: .clang-format +# To prevent clang-format on a section of code, use the following: +# // clang-format off +# my code that i dont want to be clang-formatted goes here +# // clang-format on +# + +# Check that clang-format is available. +CLANG_FORMAT=`command -v clang-format` +if [ -z $CLANG_FORMAT ]; then + # Maybe path isn't set. Try a well known location. + CLANG_FORMAT=/usr/bin/clang-format + if [ ! -x "$CLANG_FORMAT" ]; then + echo "Unable to locate clang-format. Try installing it: sudo apt-get install clang-format" + fi +fi + +# Find all hpp and cpp files excluding some directories. +files=$(git ls-files ${1} | egrep -v '^(build-tool|docker|docs|generated|third-party)' | egrep '\.[ch](pp)?$') + +num_files_changed=0 + +for file in $files; do + echo "[CLANG-FORMAT] ${file}" + # Run clang-format in-place to update the file + $CLANG_FORMAT -style=file ${file} -i + if [ $? -ne 0 ]; then + echo "Error: Execution of $CLANG_FORMAT failed." + exit -1 + else + ((num_files_changed++)) + fi +done + +echo "${num_files_changed} file(s) changed" +exit ${num_files_changed} |