Skip to content

Commit

Permalink
clang-format 18 (#571)
Browse files Browse the repository at this point in the history
see: awslabs/aws-c-common#1113

remove the `./scripts/format-c.py` script in favor of having the same `./format-check.py -i` script in the same place as every other repo
  • Loading branch information
graebm authored Jun 13, 2024
1 parent e88cce2 commit a486d70
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 56 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ on:
jobs:
clang-format:

runs-on: ubuntu-20.04 # latest
runs-on: ubuntu-24.04 # latest

steps:
- name: Checkout Sources
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: clang-format lint
uses: DoozyX/[email protected]
with:
# List of extensions to check
extensions: c,h
run: |
./format-check.py
autopep8:
runs-on: ubuntu-20.04 # latest
Expand Down
46 changes: 46 additions & 0 deletions format-check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
import argparse
import os
from pathlib import Path
import re
from subprocess import list2cmdline, run
from tempfile import NamedTemporaryFile

CLANG_FORMAT_VERSION = '18.1.6'

INCLUDE_REGEX = re.compile(r'^source/.*\.(c|h)$')
EXCLUDE_REGEX = re.compile(r'^$')

arg_parser = argparse.ArgumentParser(description="Check with clang-format")
arg_parser.add_argument('-i', '--inplace-edit', action='store_true',
help="Edit files inplace")
args = arg_parser.parse_args()

os.chdir(Path(__file__).parent)

# create file containing list of all files to format
filepaths_file = NamedTemporaryFile(delete=False)
for dirpath, dirnames, filenames in os.walk('.'):
for filename in filenames:
# our regexes expect filepath to use forward slash
filepath = Path(dirpath, filename).as_posix()
if not INCLUDE_REGEX.match(filepath):
continue
if EXCLUDE_REGEX.match(filepath):
continue

filepaths_file.write(f"{filepath}\n".encode())
filepaths_file.close()

# use pipx to run clang-format from PyPI
# this is a simple way to run the same clang-format version regardless of OS
cmd = ['pipx', 'run', f'clang-format=={CLANG_FORMAT_VERSION}',
f'--files={filepaths_file.name}']
if args.inplace_edit:
cmd += ['-i']
else:
cmd += ['--Werror', '--dry-run']

print(f"{Path.cwd()}$ {list2cmdline(cmd)}")
if run(cmd).returncode:
exit(1)
24 changes: 0 additions & 24 deletions format-check.sh

This file was deleted.

9 changes: 5 additions & 4 deletions guides/dev/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,11 @@ the code is formatted correctly.
`autopep8` is used for python code. You installed this earlier via `requirements-dev.txt`.
For C code `clang-format` is used (specifically version 9).
To install this on Mac using homebrew, run:
For C code `clang-format` is used. You need to install an exact version (see `CLANG_FORMAT_VERSION=...` line at the top of [format-check.py](../../format-check.py)) via [pipx](https://github.com/pypa/pipx). Doing this on MacOS looks like:
```sh
(.venv) $ brew install llvm@9
(.venv) $ brew install pipx
(.venv) $ pipx ensurepath
(.venv) $ pipx install clang-format==<CLANG_FORMAT_VERSION from format-check.py>
```
Use helper scripts to automatically format your code (or configure your IDE to do it):
Expand All @@ -194,7 +195,7 @@ Use helper scripts to automatically format your code (or configure your IDE to d
(.venv) $ python3 scripts/format-python.py
# just format C files
(.venv) $ python3 scripts/format-c.py
(.venv) $ python3 format-check.py -i
```
## Using an IDE
Expand Down
2 changes: 1 addition & 1 deletion scripts/format-all.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

utils.chdir_project_root()

utils.run('python3 scripts/format-c.py')
utils.run('python3 format-check.py -i')
utils.run('python3 scripts/format-python.py')
16 changes: 0 additions & 16 deletions scripts/format-c.py

This file was deleted.

6 changes: 3 additions & 3 deletions source/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ PyObject *PyErr_AwsLastError(void) {
}

#define AWS_DEFINE_ERROR_INFO_CRT(CODE, STR) \
[(CODE)-AWS_ERROR_ENUM_BEGIN_RANGE(AWS_CRT_PYTHON_PACKAGE_ID)] = AWS_DEFINE_ERROR_INFO(CODE, STR, "aws-crt-python")
[(CODE) - AWS_ERROR_ENUM_BEGIN_RANGE(AWS_CRT_PYTHON_PACKAGE_ID)] = \
AWS_DEFINE_ERROR_INFO(CODE, STR, "aws-crt-python")

/* clang-format off */
static struct aws_error_info s_errors[] = {
Expand Down Expand Up @@ -655,8 +656,7 @@ static void s_install_crash_handler(void) {
* Definitions
******************************************************************************/

#define AWS_PY_METHOD_DEF(NAME, FLAGS) \
{ #NAME, aws_py_##NAME, (FLAGS), NULL }
#define AWS_PY_METHOD_DEF(NAME, FLAGS) {#NAME, aws_py_##NAME, (FLAGS), NULL}

static PyMethodDef s_module_methods[] = {
/* Common */
Expand Down
8 changes: 6 additions & 2 deletions source/mqtt5_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,9 @@ static void s_on_publish_received(const struct aws_mqtt5_packet_publish_view *pu
/* i */ (int)(publish_packet->payload_format ? *publish_packet->payload_format : 0),
/* O */ publish_packet->message_expiry_interval_seconds ? Py_True : Py_False,
/* I */
(unsigned int)(publish_packet->message_expiry_interval_seconds ? *publish_packet->message_expiry_interval_seconds : 0),
(unsigned int)(publish_packet->message_expiry_interval_seconds
? *publish_packet->message_expiry_interval_seconds
: 0),
/* O */ publish_packet->topic_alias ? Py_True : Py_False,
/* H */ (unsigned short)(publish_packet->topic_alias ? *publish_packet->topic_alias : 0),
/* s */ publish_packet->response_topic ? publish_packet->response_topic->ptr : NULL,
Expand Down Expand Up @@ -544,7 +546,9 @@ static void s_lifecycle_event_disconnection(
/* i */ (int)(disconnect ? disconnect->reason_code : 0),
/* O */ (disconnect && disconnect->session_expiry_interval_seconds) ? Py_True : Py_False,
/* I */
(unsigned int)((disconnect && disconnect->session_expiry_interval_seconds) ? *disconnect->session_expiry_interval_seconds : 0),
(unsigned int)((disconnect && disconnect->session_expiry_interval_seconds)
? *disconnect->session_expiry_interval_seconds
: 0),
/* s */ (disconnect && disconnect->reason_string) ? disconnect->reason_string->ptr : NULL,
/* # */ (disconnect && disconnect->reason_string) ? disconnect->reason_string->len : 0,
/* O */ user_property_count > 0 ? user_properties_list : Py_None,
Expand Down

0 comments on commit a486d70

Please sign in to comment.