Skip to content

Commit 468ebc5

Browse files
committed
Merge branch 'kn/ci-clang-format'
A CI job that use clang-format to check coding style issues in new code has been added. * kn/ci-clang-format: ci/style-check: add `RemoveBracesLLVM` in CI job check-whitespace: detect if no base_commit is provided ci: run style check on GitHub and GitLab clang-format: formalize some of the spacing rules clang-format: avoid spacing around bitfield colon clang-format: indent preprocessor directives after hash
2 parents 90139ae + 1b8f306 commit 468ebc5

File tree

6 files changed

+120
-3
lines changed

6 files changed

+120
-3
lines changed

.clang-format

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ AlwaysBreakAfterReturnType: None
7272
BinPackArguments: true
7373
BinPackParameters: true
7474

75+
# Add no space around the bit field
76+
# unsigned bf:2;
77+
BitFieldColonSpacing: None
78+
7579
# Attach braces to surrounding context except break before braces on function
7680
# definitions.
7781
# void foo()
@@ -96,6 +100,12 @@ BreakStringLiterals: false
96100
# Switch statement body is always indented one level more than case labels.
97101
IndentCaseLabels: false
98102

103+
# Indents directives before the hash.
104+
# #if FOO
105+
# # include <foo>
106+
# #endif
107+
IndentPPDirectives: AfterHash
108+
99109
# Don't indent a function definition or declaration if it is wrapped after the
100110
# type
101111
IndentWrappedFunctionNames: false
@@ -108,11 +118,18 @@ PointerAlignment: Right
108118
# x = (int32)y; not x = (int32) y;
109119
SpaceAfterCStyleCast: false
110120

121+
# No space is inserted after the logical not operator
122+
SpaceAfterLogicalNot: false
123+
111124
# Insert spaces before and after assignment operators
112125
# int a = 5; not int a=5;
113126
# a += 42; a+=42;
114127
SpaceBeforeAssignmentOperators: true
115128

129+
# Spaces will be removed before case colon.
130+
# case 1: break; not case 1 : break;
131+
SpaceBeforeCaseColon: false
132+
116133
# Put a space before opening parentheses only after control statement keywords.
117134
# void f() {
118135
# if (true) {
@@ -124,6 +141,14 @@ SpaceBeforeParens: ControlStatements
124141
# Don't insert spaces inside empty '()'
125142
SpaceInEmptyParentheses: false
126143

144+
# No space before first '[' in arrays
145+
# int a[5][5]; not int a [5][5];
146+
SpaceBeforeSquareBrackets: false
147+
148+
# No space will be inserted into {}
149+
# while (true) {} not while (true) { }
150+
SpaceInEmptyBlock: false
151+
127152
# The number of spaces before trailing line comments (// - comments).
128153
# This does not affect trailing block comments (/* - comments).
129154
SpacesBeforeTrailingComments: 1

.github/workflows/check-style.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: check-style
2+
3+
# Get the repository with all commits to ensure that we can analyze
4+
# all of the commits contributed via the Pull Request.
5+
6+
on:
7+
pull_request:
8+
types: [opened, synchronize]
9+
10+
# Avoid unnecessary builds. Unlike the main CI jobs, these are not
11+
# ci-configurable (but could be).
12+
concurrency:
13+
group: ${{ github.workflow }}-${{ github.ref }}
14+
cancel-in-progress: true
15+
16+
jobs:
17+
check-style:
18+
env:
19+
CC: clang
20+
jobname: ClangFormat
21+
runs-on: ubuntu-latest
22+
steps:
23+
- uses: actions/checkout@v4
24+
with:
25+
fetch-depth: 0
26+
27+
- run: ci/install-dependencies.sh
28+
29+
- name: git clang-format
30+
continue-on-error: true
31+
id: check_out
32+
run: |
33+
./ci/run-style-check.sh \
34+
"${{github.event.pull_request.base.sha}}"

.gitlab-ci.yml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,31 @@ check-whitespace:
118118
image: ubuntu:latest
119119
before_script:
120120
- ./ci/install-dependencies.sh
121+
# Since $CI_MERGE_REQUEST_TARGET_BRANCH_SHA is only defined for merged
122+
# pipelines, we fallback to $CI_MERGE_REQUEST_DIFF_BASE_SHA, which should
123+
# be defined in all pipelines.
121124
script:
122-
- ./ci/check-whitespace.sh "$CI_MERGE_REQUEST_TARGET_BRANCH_SHA"
125+
- |
126+
R=${CI_MERGE_REQUEST_TARGET_BRANCH_SHA-${CI_MERGE_REQUEST_DIFF_BASE_SHA:?}} || exit
127+
./ci/check-whitespace.sh "$R"
128+
rules:
129+
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
130+
131+
check-style:
132+
image: ubuntu:latest
133+
allow_failure: true
134+
variables:
135+
CC: clang
136+
jobname: ClangFormat
137+
before_script:
138+
- ./ci/install-dependencies.sh
139+
# Since $CI_MERGE_REQUEST_TARGET_BRANCH_SHA is only defined for merged
140+
# pipelines, we fallback to $CI_MERGE_REQUEST_DIFF_BASE_SHA, which should
141+
# be defined in all pipelines.
142+
script:
143+
- |
144+
R=${CI_MERGE_REQUEST_TARGET_BRANCH_SHA-${CI_MERGE_REQUEST_DIFF_BASE_SHA:?}} || exit
145+
./ci/run-style-check.sh "$R"
123146
rules:
124147
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
125148

ci/check-whitespace.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ baseCommit=$1
99
outputFile=$2
1010
url=$3
1111

12-
if test "$#" -ne 1 && test "$#" -ne 3
12+
if test "$#" -ne 1 && test "$#" -ne 3 || test -z "$1"
1313
then
1414
echo "USAGE: $0 <BASE_COMMIT> [<OUTPUT_FILE> <URL>]"
1515
exit 1
@@ -21,6 +21,12 @@ commitText=
2121
commitTextmd=
2222
goodParent=
2323

24+
if ! git rev-parse --quiet --verify "${baseCommit}"
25+
then
26+
echo "Invalid <BASE_COMMIT> '${baseCommit}'"
27+
exit 1
28+
fi
29+
2430
while read dash sha etc
2531
do
2632
case "${dash}" in
@@ -67,7 +73,7 @@ then
6773
goodParent=${baseCommit: 0:7}
6874
fi
6975

70-
echo "A whitespace issue was found in onen of more of the commits."
76+
echo "A whitespace issue was found in one or more of the commits."
7177
echo "Run the following command to resolve whitespace issues:"
7278
echo "git rebase --whitespace=fix ${goodParent}"
7379

ci/install-dependencies.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ macos-*)
8787
esac
8888

8989
case "$jobname" in
90+
ClangFormat)
91+
sudo apt-get -q update
92+
sudo apt-get -q -y install clang-format
93+
;;
9094
StaticAnalysis)
9195
sudo apt-get -q update
9296
sudo apt-get -q -y install coccinelle libcurl4-openssl-dev libssl-dev \

ci/run-style-check.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/sh
2+
#
3+
# Perform style check
4+
#
5+
6+
baseCommit=$1
7+
8+
# Remove optional braces of control statements (if, else, for, and while)
9+
# according to the LLVM coding style. This avoids braces on simple
10+
# single-statement bodies of statements but keeps braces if one side of
11+
# if/else if/.../else cascade has multi-statement body.
12+
#
13+
# As this rule comes with a warning [1], we want to experiment with it
14+
# before adding it in-tree. since the CI job for the style check is allowed
15+
# to fail, appending the rule here allows us to validate its efficacy.
16+
# While also ensuring that end-users are not affected directly.
17+
#
18+
# [1]: https://clang.llvm.org/docs/ClangFormatStyleOptions.html#removebracesllvm
19+
{
20+
cat .clang-format
21+
echo "RemoveBracesLLVM: true"
22+
} >/tmp/clang-format-rules
23+
24+
git clang-format --style=file:/tmp/clang-format-rules \
25+
--diff --extensions c,h "$baseCommit"

0 commit comments

Comments
 (0)