From a18aad21de45774898e9e430247af5e8ede2838f Mon Sep 17 00:00:00 2001 From: Michael Graeb Date: Wed, 12 Jun 2024 10:33:22 -0700 Subject: [PATCH] clang-format 18 see: https://github.com/awslabs/aws-c-common/pull/1113 --- .github/workflows/clang-format.yml | 10 +++--- format-check.py | 47 ++++++++++++++++++++++++++++ format-check.sh | 24 -------------- include/aws/http/connection.h | 5 ++- include/aws/http/private/h2_frames.h | 4 ++- include/aws/http/proxy.h | 14 ++++----- include/aws/http/request_response.h | 4 ++- include/aws/http/server.h | 13 +++++--- source/http.c | 2 +- tests/test_h1_client.c | 10 +++--- tests/test_h2_client.c | 5 ++- tests/test_h2_encoder.c | 3 +- tests/test_h2_headers.c | 3 +- tests/test_localhost_integ.c | 5 ++- tests/test_message.c | 5 ++- tests/test_stream_manager.c | 5 ++- tests/test_websocket_handler.c | 4 ++- 17 files changed, 106 insertions(+), 57 deletions(-) create mode 100755 format-check.py delete mode 100755 format-check.sh diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml index cca2eb7cb..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@v1 + 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 eae26b787..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 bin source include tests -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/http/connection.h b/include/aws/http/connection.h index 031957ef0..067e94998 100644 --- a/include/aws/http/connection.h +++ b/include/aws/http/connection.h @@ -455,7 +455,10 @@ struct aws_http2_setting { * Initializes aws_http_client_connection_options with default values. */ #define AWS_HTTP_CLIENT_CONNECTION_OPTIONS_INIT \ - { .self_size = sizeof(struct aws_http_client_connection_options), .initial_window_size = SIZE_MAX, } + { \ + .self_size = sizeof(struct aws_http_client_connection_options), \ + .initial_window_size = SIZE_MAX, \ + } AWS_EXTERN_C_BEGIN diff --git a/include/aws/http/private/h2_frames.h b/include/aws/http/private/h2_frames.h index 23c1daf1e..6affd62a4 100644 --- a/include/aws/http/private/h2_frames.h +++ b/include/aws/http/private/h2_frames.h @@ -50,7 +50,9 @@ struct aws_h2err { }; #define AWS_H2ERR_SUCCESS \ - (struct aws_h2err) { .h2_code = 0, .aws_code = 0 } + (struct aws_h2err) { \ + .h2_code = 0, .aws_code = 0 \ + } #define AWS_H2_PAYLOAD_MAX (0x00FFFFFF) /* must fit in 3 bytes */ #define AWS_H2_WINDOW_UPDATE_MAX (0x7FFFFFFF) /* cannot use high bit */ diff --git a/include/aws/http/proxy.h b/include/aws/http/proxy.h index 28fdfb045..4e23d30db 100644 --- a/include/aws/http/proxy.h +++ b/include/aws/http/proxy.h @@ -161,10 +161,10 @@ typedef struct aws_string *(aws_http_proxy_negotiation_get_token_sync_fn)(void * * Synchronous (for now) callback function to fetch a token used in modifying CONNECT request. Includes a (byte string) * context intended to be used as part of a challenge-response flow. */ -typedef struct aws_string *(aws_http_proxy_negotiation_get_challenge_token_sync_fn)( - void *user_data, - const struct aws_byte_cursor *challenge_context, - int *out_error_code); +typedef struct aws_string *( + aws_http_proxy_negotiation_get_challenge_token_sync_fn)(void *user_data, + const struct aws_byte_cursor *challenge_context, + int *out_error_code); /** * Proxy negotiation logic must call this function to indicate an unsuccessful outcome @@ -307,9 +307,9 @@ struct aws_http_proxy_negotiator { /*********************************************************************************************/ -typedef struct aws_http_proxy_negotiator *(aws_http_proxy_strategy_create_negotiator_fn)( - struct aws_http_proxy_strategy *proxy_strategy, - struct aws_allocator *allocator); +typedef struct aws_http_proxy_negotiator *( + aws_http_proxy_strategy_create_negotiator_fn)(struct aws_http_proxy_strategy *proxy_strategy, + struct aws_allocator *allocator); struct aws_http_proxy_strategy_vtable { aws_http_proxy_strategy_create_negotiator_fn *create_negotiator; diff --git a/include/aws/http/request_response.h b/include/aws/http/request_response.h index 73c190050..d0d297724 100644 --- a/include/aws/http/request_response.h +++ b/include/aws/http/request_response.h @@ -501,7 +501,9 @@ struct aws_http2_stream_write_data_options { }; #define AWS_HTTP_REQUEST_HANDLER_OPTIONS_INIT \ - { .self_size = sizeof(struct aws_http_request_handler_options), } + { \ + .self_size = sizeof(struct aws_http_request_handler_options), \ + } AWS_EXTERN_C_BEGIN diff --git a/include/aws/http/server.h b/include/aws/http/server.h index 03893355d..f7e595f15 100644 --- a/include/aws/http/server.h +++ b/include/aws/http/server.h @@ -112,15 +112,18 @@ struct aws_http_server_options { * Initializes aws_http_server_options with default values. */ #define AWS_HTTP_SERVER_OPTIONS_INIT \ - { .self_size = sizeof(struct aws_http_server_options), .initial_window_size = SIZE_MAX, } + { \ + .self_size = sizeof(struct aws_http_server_options), \ + .initial_window_size = SIZE_MAX, \ + } /** * Invoked at the start of an incoming request. * To process the request, the user must create a request handler stream and return it to the connection. * If NULL is returned, the request will not be processed and the last error will be reported as the reason for failure. */ -typedef struct aws_http_stream *( - aws_http_on_incoming_request_fn)(struct aws_http_connection *connection, void *user_data); +typedef struct aws_http_stream *(aws_http_on_incoming_request_fn)(struct aws_http_connection *connection, + void *user_data); typedef void(aws_http_on_server_connection_shutdown_fn)( struct aws_http_connection *connection, @@ -163,7 +166,9 @@ struct aws_http_server_connection_options { * Initializes aws_http_server_connection_options with default values. */ #define AWS_HTTP_SERVER_CONNECTION_OPTIONS_INIT \ - { .self_size = sizeof(struct aws_http_server_connection_options), } + { \ + .self_size = sizeof(struct aws_http_server_connection_options), \ + } AWS_EXTERN_C_BEGIN diff --git a/source/http.c b/source/http.c index af61c5151..551e97db7 100644 --- a/source/http.c +++ b/source/http.c @@ -12,7 +12,7 @@ #include -#define AWS_DEFINE_ERROR_INFO_HTTP(CODE, STR) [(CODE)-0x0800] = AWS_DEFINE_ERROR_INFO(CODE, STR, "aws-c-http") +#define AWS_DEFINE_ERROR_INFO_HTTP(CODE, STR) [(CODE) - 0x0800] = AWS_DEFINE_ERROR_INFO(CODE, STR, "aws-c-http") /* clang-format off */ static struct aws_error_info s_errors[] = { diff --git a/tests/test_h1_client.c b/tests/test_h1_client.c index 89ac98520..8c32a13c6 100644 --- a/tests/test_h1_client.c +++ b/tests/test_h1_client.c @@ -1318,11 +1318,11 @@ static int s_can_parse_as_chunked_encoding( } } long chunk_size = strtol((char *)chunk_ascii_hex, 0, 16); - long total_chunk_size_with_overhead = (long) - (match_cursor.ptr - request_cursor.ptr /* size of the chunk in ascii hex */ - + crlf_cursor.len /* size of the crlf */ - + chunk_size /* size of the payload */ - + crlf_cursor.len); /* size of the chunk terminating crlf */ + long total_chunk_size_with_overhead = + (long)(match_cursor.ptr - request_cursor.ptr /* size of the chunk in ascii hex */ + + crlf_cursor.len /* size of the crlf */ + + chunk_size /* size of the payload */ + + crlf_cursor.len); /* size of the chunk terminating crlf */ /* 0 length chunk signals end of stream. Check for the terminatino string and exit with success */ if (0 == chunk_size) { diff --git a/tests/test_h2_client.c b/tests/test_h2_client.c index 6794c6e2b..4a59a5a27 100644 --- a/tests/test_h2_client.c +++ b/tests/test_h2_client.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), \ + } struct connection_user_data { struct aws_allocator *allocator; diff --git a/tests/test_h2_encoder.c b/tests/test_h2_encoder.c index ad0bb34f4..3b3aed591 100644 --- a/tests/test_h2_encoder.c +++ b/tests/test_h2_encoder.c @@ -28,7 +28,8 @@ static int s_fixture_clean_up(struct aws_allocator *allocator, int setup_res, vo #define DEFINE_STATIC_HEADER(_key, _value, _behavior) \ { \ - .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(_key), .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(_value), \ + .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(_key), \ + .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(_value), \ .compression = AWS_HTTP_HEADER_COMPRESSION_##_behavior, \ } diff --git a/tests/test_h2_headers.c b/tests/test_h2_headers.c index c045ca1bc..9691558e5 100644 --- a/tests/test_h2_headers.c +++ b/tests/test_h2_headers.c @@ -169,7 +169,8 @@ static int s_header_test_after(struct aws_allocator *allocator, int setup_res, v #define DEFINE_STATIC_HEADER(_key, _value, _behavior) \ { \ - .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(_key), .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(_value), \ + .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(_key), \ + .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(_value), \ .compression = AWS_HTTP_HEADER_COMPRESSION_##_behavior, \ } diff --git a/tests/test_localhost_integ.c b/tests/test_localhost_integ.c index 709a02823..dd256b402 100644 --- a/tests/test_localhost_integ.c +++ b/tests/test_localhost_integ.c @@ -94,7 +94,10 @@ struct tester { static struct tester s_tester; #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), \ + } enum { TESTER_TIMEOUT_SEC = 60, /* Give enough time for non-sudo users to enter password */ diff --git a/tests/test_message.c b/tests/test_message.c index 541527656..eb42bdf40 100644 --- a/tests/test_message.c +++ b/tests/test_message.c @@ -13,7 +13,10 @@ AWS_TEST_CASE(NAME, s_test_##NAME); \ 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(message_sanity_check) { (void)ctx; diff --git a/tests/test_stream_manager.c b/tests/test_stream_manager.c index 68d8a7273..991b8efb8 100644 --- a/tests/test_stream_manager.c +++ b/tests/test_stream_manager.c @@ -34,7 +34,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 sm_tester_options { struct aws_allocator *alloc; diff --git a/tests/test_websocket_handler.c b/tests/test_websocket_handler.c index e8989f7a6..98db51305 100644 --- a/tests/test_websocket_handler.c +++ b/tests/test_websocket_handler.c @@ -37,7 +37,9 @@ struct incoming_frame { bool is_complete; }; -static struct tester_options { bool manual_window_update; } s_tester_options; +static struct tester_options { + bool manual_window_update; +} s_tester_options; struct tester { struct aws_allocator *alloc;