diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml index b0bdb1865..36388d688 100644 --- a/.github/workflows/clang-format.yml +++ b/.github/workflows/clang-format.yml @@ -5,14 +5,12 @@ on: [push] jobs: clang-format: - runs-on: ubuntu-20.04 # latest + runs-on: ubuntu-24.04 # latest steps: - name: Checkout Sources uses: actions/checkout@v4 - name: clang-format lint - uses: DoozyX/clang-format-lint-action@v0.3.1 - with: - # List of extensions to check - extensions: c,h + run: | + ./format-check.py diff --git a/format-check.py b/format-check.py new file mode 100755 index 000000000..b9e3520cc --- /dev/null +++ b/format-check.py @@ -0,0 +1,47 @@ +#!/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'^(include|source|tests|verification)/.*\.(c|h|inl)$') +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) diff --git a/format-check.sh b/format-check.sh deleted file mode 100755 index 034e7216c..000000000 --- a/format-check.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/bash - -if [[ -z $CLANG_FORMAT ]] ; then - CLANG_FORMAT=clang-format -fi - -if NOT type $CLANG_FORMAT 2> /dev/null ; then - echo "No appropriate clang-format found." - exit 1 -fi - -FAIL=0 -SOURCE_FILES=`find source include tests samples -type f \( -name '*.h' -o -name '*.c' \)` -for i in $SOURCE_FILES -do - $CLANG_FORMAT -output-replacements-xml $i | grep -c " /dev/null - if [ $? -ne 1 ] - then - echo "$i failed clang-format check." - FAIL=1 - fi -done - -exit $FAIL diff --git a/include/aws/s3/private/s3_util.h b/include/aws/s3/private/s3_util.h index 0a3c885c3..b5aaa4e1c 100644 --- a/include/aws/s3/private/s3_util.h +++ b/include/aws/s3/private/s3_util.h @@ -22,12 +22,12 @@ #else # define ASSERT_SYNCED_DATA_LOCK_HELD(object) #endif -#define KB_TO_BYTES(kb) ((kb)*1024) -#define MB_TO_BYTES(mb) ((mb)*1024 * 1024) -#define GB_TO_BYTES(gb) ((gb)*1024 * 1024 * 1024ULL) +#define KB_TO_BYTES(kb) ((kb) * 1024) +#define MB_TO_BYTES(mb) ((mb) * 1024 * 1024) +#define GB_TO_BYTES(gb) ((gb) * 1024 * 1024 * 1024ULL) -#define MS_TO_NS(ms) ((uint64_t)(ms)*1000000) -#define SEC_TO_NS(ms) ((uint64_t)(ms)*1000000000) +#define MS_TO_NS(ms) ((uint64_t)(ms) * 1000000) +#define SEC_TO_NS(ms) ((uint64_t)(ms) * 1000000000) struct aws_allocator; struct aws_http_stream; diff --git a/include/aws/s3/s3_client.h b/include/aws/s3/s3_client.h index 71d899d54..0989934c6 100644 --- a/include/aws/s3/s3_client.h +++ b/include/aws/s3/s3_client.h @@ -325,12 +325,12 @@ typedef int(aws_s3_meta_request_upload_review_fn)( * * @return The aws_s3express_credentials_provider. */ -typedef struct aws_s3express_credentials_provider *(aws_s3express_provider_factory_fn)( - struct aws_allocator *allocator, - struct aws_s3_client *client, - aws_simple_completion_callback on_provider_shutdown_callback, - void *shutdown_user_data, - void *factory_user_data); +typedef struct aws_s3express_credentials_provider *( + aws_s3express_provider_factory_fn)(struct aws_allocator *allocator, + struct aws_s3_client *client, + aws_simple_completion_callback on_provider_shutdown_callback, + void *shutdown_user_data, + void *factory_user_data); /* Keepalive properties are TCP only. * If interval or timeout are zero, then default values are used. diff --git a/tests/s3_client_test.c b/tests/s3_client_test.c index 3b4f478d2..cba98096d 100644 --- a/tests/s3_client_test.c +++ b/tests/s3_client_test.c @@ -16,7 +16,10 @@ static int s_test_##NAME(struct aws_allocator *allocator, void *ctx) #define DEFINE_HEADER(NAME, VALUE) \ - { .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(NAME), .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(VALUE), } + { \ + .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(NAME), \ + .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(VALUE), \ + } static void s_init_mock_s3_request_upload_part_timeout( struct aws_s3_request *mock_request, diff --git a/tests/s3_meta_request_test.c b/tests/s3_meta_request_test.c index d837a179c..c8721b872 100644 --- a/tests/s3_meta_request_test.c +++ b/tests/s3_meta_request_test.c @@ -20,7 +20,10 @@ static int s_test_##NAME(struct aws_allocator *allocator, void *ctx) #define DEFINE_HEADER(NAME, VALUE) \ - { .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(NAME), .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(VALUE), } + { \ + .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(NAME), \ + .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(VALUE), \ + } TEST_CASE(meta_request_auto_ranged_get_new_error_handling) { (void)ctx; diff --git a/tests/s3_mock_server_s3express_provider_test.c b/tests/s3_mock_server_s3express_provider_test.c index 7748babf0..7fc12efca 100644 --- a/tests/s3_mock_server_s3express_provider_test.c +++ b/tests/s3_mock_server_s3express_provider_test.c @@ -20,7 +20,10 @@ static int s_test_##NAME(struct aws_allocator *allocator, void *ctx) #define DEFINE_HEADER(NAME, VALUE) \ - { .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(NAME), .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(VALUE), } + { \ + .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(NAME), \ + .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(VALUE), \ + } static uint64_t s_bg_refresh_secs_override = 60; diff --git a/tests/s3_mock_server_tests.c b/tests/s3_mock_server_tests.c index b9b336d28..deab3e64e 100644 --- a/tests/s3_mock_server_tests.c +++ b/tests/s3_mock_server_tests.c @@ -16,7 +16,10 @@ static int s_test_##NAME(struct aws_allocator *allocator, void *ctx) #define DEFINE_HEADER(NAME, VALUE) \ - { .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(NAME), .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(VALUE), } + { \ + .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(NAME), \ + .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(VALUE), \ + } static int s_validate_mpu_mock_server_metrics(struct aws_array_list *metrics_list) { /* Check the size of the metrics should be the same as the number of requests, which should be create MPU, two diff --git a/tests/s3_parallel_read_stream_test.c b/tests/s3_parallel_read_stream_test.c index a79fb65ac..17e16afda 100644 --- a/tests/s3_parallel_read_stream_test.c +++ b/tests/s3_parallel_read_stream_test.c @@ -24,7 +24,10 @@ static int s_test_##NAME(struct aws_allocator *allocator, void *ctx) #define DEFINE_HEADER(NAME, VALUE) \ - { .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(NAME), .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(VALUE), } + { \ + .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(NAME), \ + .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(VALUE), \ + } #define ONE_SEC_IN_NS ((uint64_t)AWS_TIMESTAMP_NANOS) #define MAX_TIMEOUT_NS (600 * ONE_SEC_IN_NS) diff --git a/tests/s3_s3express_client_test.c b/tests/s3_s3express_client_test.c index cfdb42b12..7c2e5a017 100644 --- a/tests/s3_s3express_client_test.c +++ b/tests/s3_s3express_client_test.c @@ -21,7 +21,10 @@ static int s_test_##NAME(struct aws_allocator *allocator, void *ctx) #define DEFINE_HEADER(NAME, VALUE) \ - { .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(NAME), .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(VALUE), } + { \ + .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(NAME), \ + .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(VALUE), \ + } struct aws_s3express_client_tester { struct aws_allocator *allocator; diff --git a/tests/s3_tester.c b/tests/s3_tester.c index 7b5636748..10342a3c4 100644 --- a/tests/s3_tester.c +++ b/tests/s3_tester.c @@ -440,7 +440,9 @@ int aws_s3_tester_init(struct aws_allocator *allocator, struct aws_s3_tester *te &tester->default_signing_config, g_test_s3_region, tester->credentials_provider); } #else - { tester->default_signing_config = tester->anonymous_signing_config; } + { + tester->default_signing_config = tester->anonymous_signing_config; + } #endif return AWS_OP_SUCCESS;