Merge branch 'kn/clang-format-updates'

Update ".clang-format" and ".editorconfig" to match our style guide
a bit better.

* kn/clang-format-updates:
  meson: add rule to run 'git clang-format'
  clang-format: add 'RemoveBracesLLVM' to the main config
  clang-format: set 'ColumnLimit' to 0
This commit is contained in:
Junio C Hamano
2025-07-14 11:19:26 -07:00
3 changed files with 28 additions and 29 deletions

View File

@@ -12,7 +12,15 @@ UseTab: Always
TabWidth: 8
IndentWidth: 8
ContinuationIndentWidth: 8
ColumnLimit: 80
# While we do want to enforce a character limit of 80 characters, we often
# allow lines to overflow that limit to prioritize readability. Setting a
# character limit here with penalties has been finicky and creates too many
# false positives.
#
# NEEDSWORK: It would be nice if we can find optimal settings to ensure we
# can re-enable the limit here.
ColumnLimit: 0
# C Language specifics
Language: Cpp
@@ -210,16 +218,11 @@ MaxEmptyLinesToKeep: 1
# No empty line at the start of a block.
KeepEmptyLinesAtTheStartOfBlocks: false
# Penalties
# This decides what order things should be done if a line is too long
PenaltyBreakAssignment: 5
PenaltyBreakBeforeFirstCallParameter: 5
PenaltyBreakComment: 5
PenaltyBreakFirstLessLess: 0
PenaltyBreakOpenParenthesis: 300
PenaltyBreakString: 5
PenaltyExcessCharacter: 10
PenaltyReturnTypeOnItsOwnLine: 300
# Don't sort #include's
SortIncludes: false
# Remove optional braces of control statements (if, else, for, and while)
# according to the LLVM coding style. This avoids braces on simple
# single-statement bodies of statements but keeps braces if one side of
# if/else if/.../else cascade has multi-statement body.
RemoveBracesLLVM: true

View File

@@ -5,21 +5,5 @@
baseCommit=$1
# Remove optional braces of control statements (if, else, for, and while)
# according to the LLVM coding style. This avoids braces on simple
# single-statement bodies of statements but keeps braces if one side of
# if/else if/.../else cascade has multi-statement body.
#
# As this rule comes with a warning [1], we want to experiment with it
# before adding it in-tree. since the CI job for the style check is allowed
# to fail, appending the rule here allows us to validate its efficacy.
# While also ensuring that end-users are not affected directly.
#
# [1]: https://clang.llvm.org/docs/ClangFormatStyleOptions.html#removebracesllvm
{
cat .clang-format
echo "RemoveBracesLLVM: true"
} >/tmp/clang-format-rules
git clang-format --style=file:/tmp/clang-format-rules \
git clang-format --style=file:.clang-format \
--diff --extensions c,h "$baseCommit"

View File

@@ -2133,6 +2133,18 @@ if headers_to_check.length() != 0 and compiler.get_argument_syntax() == 'gcc'
alias_target('check-headers', hdr_check)
endif
git_clang_format = find_program('git-clang-format', required: false, native: true)
if git_clang_format.found()
run_target('style',
command: [
git_clang_format,
'--style', 'file',
'--diff',
'--extensions', 'c,h'
]
)
endif
foreach key, value : {
'DIFF': diff.full_path(),
'GIT_SOURCE_DIR': meson.project_source_root(),