Skip to content

Commit 39b8a50

Browse files
authored
clang-format 18 (#797)
1 parent 44cc86f commit 39b8a50

File tree

3 files changed

+50
-31
lines changed

3 files changed

+50
-31
lines changed

.github/workflows/clang-format.yml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@ on:
99
jobs:
1010
clang-format:
1111

12-
runs-on: ubuntu-20.04 # latest
12+
runs-on: ubuntu-24.04 # latest
1313

1414
steps:
1515
- name: Checkout Sources
16-
uses: actions/checkout@v3
16+
uses: actions/checkout@v4
1717

1818
- name: clang-format lint
19-
uses: DoozyX/clang-format-lint-action@v0.3.1
20-
with:
21-
# List of extensions to check
22-
extensions: c,h
23-
exclude: './cmake'
19+
run: |
20+
./format-check.py

format-check.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python3
2+
import argparse
3+
import os
4+
from pathlib import Path
5+
import re
6+
from subprocess import list2cmdline, run
7+
from tempfile import NamedTemporaryFile
8+
9+
CLANG_FORMAT_VERSION = '18.1.6'
10+
11+
INCLUDE_REGEX = re.compile(r'^src/native/.*\.(c|h|inl)$')
12+
EXCLUDE_REGEX = re.compile(r'^$')
13+
14+
arg_parser = argparse.ArgumentParser(description="Check with clang-format")
15+
arg_parser.add_argument('-i', '--inplace-edit', action='store_true',
16+
help="Edit files inplace")
17+
args = arg_parser.parse_args()
18+
19+
os.chdir(Path(__file__).parent)
20+
21+
# create file containing list of all files to format
22+
filepaths_file = NamedTemporaryFile(delete=False)
23+
for dirpath, dirnames, filenames in os.walk('.'):
24+
for filename in filenames:
25+
# our regexes expect filepath to use forward slash
26+
filepath = Path(dirpath, filename).as_posix()
27+
if not INCLUDE_REGEX.match(filepath):
28+
continue
29+
if EXCLUDE_REGEX.match(filepath):
30+
continue
31+
32+
filepaths_file.write(f"{filepath}\n".encode())
33+
filepaths_file.close()
34+
35+
# use pipx to run clang-format from PyPI
36+
# this is a simple way to run the same clang-format version regardless of OS
37+
cmd = ['pipx', 'run', f'clang-format=={CLANG_FORMAT_VERSION}',
38+
f'--files={filepaths_file.name}']
39+
if args.inplace_edit:
40+
cmd += ['-i']
41+
else:
42+
cmd += ['--Werror', '--dry-run']
43+
44+
print(f"{Path.cwd()}$ {list2cmdline(cmd)}")
45+
if run(cmd).returncode:
46+
exit(1)

format-check.sh

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)