From 0255a974734bf1b4388f68dd6b3940b31f778f3b Mon Sep 17 00:00:00 2001 From: Evan Hunt Date: Thu, 13 Feb 2020 10:16:25 -0800 Subject: [PATCH] revise .clang-format and add a C formatting script in util - add util/cformat.sh, which runs clang-format on all C files with the default .clang-format, and on all header files with a slightly modified version. - use correct bracing after multi-line control statements - stop aligning variable declarations to avoid problems with pointer alignment, but retain aligned declarations in header files so that struct definitions look cleaner. - static function prototypes in C files can skip the line break after the return type, but function prototypes in header files still have the line break. - don't break-before-brace in function definitions. ISC style calls for braces on the same line when function parameters fit on a single line, and a line break if they don't, but clang-format doesn't yet support that distinction. one-line function definitions are about four times more common than multi-line, so let's use the option that deviates less. --- .clang-format | 29 ++++++++++++++++++++--------- util/cformat.sh | 28 ++++++++++++++++++++++++++++ util/copyrights | 1 + 3 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 util/cformat.sh diff --git a/.clang-format b/.clang-format index 7e2dbc5094..8f653f68d1 100644 --- a/.clang-format +++ b/.clang-format @@ -1,20 +1,35 @@ BasedOnStyle: LLVM IndentWidth: 8 UseTab: Always -BreakBeforeBraces: Linux +BreakBeforeBraces: Custom +BraceWrapping: + AfterClass: false + AfterEnum: false + AfterStruct: false + AfterUnion: false + AfterControlStatement: MultiLine + AfterFunction: false # should also be MultiLine, but not yet supported + AfterExternBlock: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true AllowShortIfStatementsOnASingleLine: false IndentCaseLabels: false -AlwaysBreakAfterReturnType: All +AlwaysBreakAfterReturnType: TopLevelDefinitions Cpp11BracedListStyle: false ColumnLimit: 80 AlignAfterOpenBracket: Align -AlignConsecutiveDeclarations: true +AlignConsecutiveDeclarations: false +AlignConsecutiveMacros: true +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true AlwaysBreakBeforeMultilineStrings: false BreakBeforeBinaryOperators: None BreakBeforeTernaryOperators: true AlignEscapedNewlines: Left DerivePointerAlignment: false PointerAlignment: Right +PointerBindsToType: false IncludeBlocks: Regroup IncludeCategories: - Regex: '^ .clang-format + +# modify header files +find bin lib -name "*.h" | + xargs clang-format --style=file --sort-includes -i + +# restore the original .clang-format file +cp -f .clang-format.bak .clang-format +rm -f .clang-format.bak diff --git a/util/copyrights b/util/copyrights index 8a8497a5f6..eb35aae950 100644 --- a/util/copyrights +++ b/util/copyrights @@ -2573,6 +2573,7 @@ ./util/COPYRIGHT.TOP X 2018,2019,2020 ./util/bindkeys.pl PERL 2009,2010,2011,2012,2014,2016,2017,2018,2019,2020 ./util/branchsync.sh SH 2013,2016,2018,2019,2020 +./util/cformat.sh SH 2020 ./util/check-ans-prereq.sh SH 2019,2020 ./util/check-categories.sh SH 2015,2016,2017,2018,2019,2020 ./util/check-changes PERL 2002,2004,2007,2012,2016,2018,2019,2020