From f2b357279928fc6b3b2e228d0a3d6ab1774cc3c3 Mon Sep 17 00:00:00 2001 From: Heikki Hellgren Date: Thu, 18 Oct 2018 15:28:40 +0300 Subject: [PATCH 1/2] Add .clang-format file for json Almost with same configuration used this far in the software. Minor changes here and there might occur. --- .clang-format | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 000000000..21a0eaaf0 --- /dev/null +++ b/.clang-format @@ -0,0 +1,138 @@ +--- +# Clang-format settings for nlohmann/json +# For more information, see https://clang.llvm.org/docs/ClangFormatStyleOptions.html + +# Language and general settings +Language: Cpp +Standard: Cpp11 +DisableFormat: false + +# Indentation +AccessModifierOffset: -2 +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 0 +IndentCaseLabels: true +IndentPPDirectives: None +IndentWidth: 4 +IndentWrappedFunctionNames: false +NamespaceIndentation: None +ObjCBlockIndentWidth: 2 +TabWidth: 4 +UseTab: Never + +# Alignment +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Right +AlignOperands: true +DerivePointerAlignment: false +PointerAlignment: Left +AlignTrailingComments: true + +# Braces +BreakBeforeBraces: Custom +Cpp11BracedListStyle: true +BraceWrapping: + AfterClass: true + AfterControlStatement: true + AfterEnum: false + AfterFunction: true + AfterNamespace: true + AfterObjCDeclaration: true + AfterStruct: true + AfterUnion: true + AfterExternBlock: false + BeforeCatch: false + BeforeElse: true + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyRecord: true + SplitEmptyNamespace: true + +# Wrapping +ColumnLimit: 120 +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: true +BreakBeforeBinaryOperators: None +BreakBeforeInheritanceComma: false +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +BinPackArguments: true +BinPackParameters: true +ExperimentalAutoDetectBinPacking: false + +# Comments +ReflowComments: false +CommentPragmas: '^ IWYU pragma:' + +# Empty lines +KeepEmptyLinesAtTheStartOfBlocks: true +MaxEmptyLinesToKeep: 1 + +# Spacing +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true + +# Includes +SortIncludes: true +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + - Regex: '^(<|"(gtest|gmock|isl|nlohmann)/)' + Priority: 3 + - Regex: '.*' + Priority: 1 +IncludeIsMainRegex: '(Test)?$' +SortUsingDeclarations: true + +# Namespaces +FixNamespaceComments: true +CompactNamespaces: false + +# Classes +ConstructorInitializerAllOnOneLineOrOnePerLine: false + +# Macros +MacroBlockBegin: '' +MacroBlockEnd: '' + +# Penalties +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 + +# Misc +RawStringFormats: + - Delimiter: pb + Language: TextProto + BasedOnStyle: google +... + From 6393acc1b0f771db28fac79f2172029d697c4061 Mon Sep 17 00:00:00 2001 From: Heikki Hellgren Date: Thu, 18 Oct 2018 18:26:05 +0300 Subject: [PATCH 2/2] Add clang-format call to pretty Makefile target --- .clang-format | 5 ----- .github/CONTRIBUTING.md | 2 +- Makefile | 10 +++++++++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/.clang-format b/.clang-format index 21a0eaaf0..b912338e4 100644 --- a/.clang-format +++ b/.clang-format @@ -129,10 +129,5 @@ PenaltyBreakString: 1000 PenaltyExcessCharacter: 1000000 PenaltyReturnTypeOnItsOwnLine: 60 -# Misc -RawStringFormats: - - Delimiter: pb - Language: TextProto - BasedOnStyle: google ... diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 4ae0cb2d7..482f0f398 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -50,7 +50,7 @@ To make changes, you need to edit the following files: ## Note - If you open a pull request, the code will be automatically tested with [Valgrind](http://valgrind.org)'s Memcheck tool to detect memory leaks. Please be aware that the execution with Valgrind _may_ in rare cases yield different behavior than running the code directly. This can result in failing unit tests which run successfully without Valgrind. -- There is a Makefile target `make pretty` which runs [Artistic Style](http://astyle.sourceforge.net) to fix indentation. If possible, run it before opening the pull request. Otherwise, we shall run it afterward. +- There is a Makefile target `make pretty` which runs [Artistic Style](http://astyle.sourceforge.net) to fix indentation and [clang-format](https://clang.llvm.org/docs/ClangFormat.html). If possible, run it before opening the pull request. Otherwise, we shall run it afterward. ## Please don't diff --git a/Makefile b/Makefile index 135db65a8..9adbc44a8 100644 --- a/Makefile +++ b/Makefile @@ -54,7 +54,7 @@ all: @echo "json_unit - create single-file test executable" @echo "pedantic_clang - run Clang with maximal warning flags" @echo "pedantic_gcc - run GCC with maximal warning flags" - @echo "pretty - beautify code with Artistic Style" + @echo "pretty - beautify code with Artistic Style and clang-format" @echo "run_benchmarks - build and run benchmarks" ########################################################################## @@ -276,9 +276,12 @@ clang_analyze: ########################################################################## # maintainer targets ########################################################################## +ASTYLE := $(shell command -v astyle 2> /dev/null) +CLANG_FORMAT := $(shell command -v clang-format 2> /dev/null) # pretty printer pretty: +ifdef ASTYLE astyle --style=allman --indent=spaces=4 --indent-modifiers \ --indent-switches --indent-preproc-block --indent-preproc-define \ --indent-col1-comments --pad-oper --pad-header --align-pointer=type \ @@ -286,6 +289,11 @@ pretty: --lineend=linux --preserve-date --suffix=none --formatted \ $(SRCS) $(AMALGAMATED_FILE) test/src/*.cpp \ benchmarks/src/benchmarks.cpp doc/examples/*.cpp +endif + +ifdef CLANG_FORMAT + clang-format -i $(SRCS) +endif # create single header file amalgamate: $(AMALGAMATED_FILE)