Merge pull request #4350 from akallabeth/format_code_patch

New code formatting rules
This commit is contained in:
David Fort 2019-05-23 16:11:54 +02:00 committed by GitHub
commit 3d346b69df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 180 additions and 53 deletions

125
.clang-format Normal file
View File

@ -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
...

View File

@ -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

View File

@ -51,6 +51,10 @@ endif()
include(CheckCmakeCompat)
# Include cmake modules
if(WITH_CLANG_FORMAT)
include(ClangFormat)
endif()
include(CheckIncludeFiles)
include(CheckLibraryExists)
include(CheckSymbolExists)

48
cmake/ClangFormat.cmake Normal file
View File

@ -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()

View File

@ -135,6 +135,7 @@ option(WITH_DEBUG_RINGBUFFER "Enable Ringbuffer debug messages" ${DEFAULT_DEBUG_
option(WITH_DEBUG_SYMBOLS "Pack debug symbols to installer" OFF)
option(WITH_CCACHE "Use ccache support if available" ON)
option(WITH_CLANG_FORMAT "Detect clang-format. run 'cmake --build . --target clangformat' to format." ON)
option(WITH_ICU "Use ICU for unicode conversion" OFF)
option(WITH_GSSAPI "Compile support for kerberos authentication. (EXPERIMENTAL)" OFF)

View File

@ -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

View File

@ -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 <file1> [<file2> ...]"
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 $?