From 4fccc79c8402b37802d8cb97899464b43dc6bbfc Mon Sep 17 00:00:00 2001 From: Michael Graeb Date: Wed, 12 Jun 2024 09:52:32 -0700 Subject: [PATCH] clang-format 18 (#1113) **Issue:** - We've used clang-format 9 since forever, but it's not easy for people to get such an old version anymore. - You really need everyone on the same exact version clang-format, even PATCH matters - It's very hard to get the same version on everyone's machine (Ubuntu 24 has 18, brew on MacOS has 18, but Amazon Linux 2 only has 11, and AL2023 only has 15) **Description of Changes:** - start using latest version of clang-format: 18.1.6 - use [pipx](https://github.com/pypa/pipx) to install and run [clang-format from PyPI](https://pypi.org/project/clang-format). This lets everyone run the same version, regardless of OS - rewrite ~format-check.sh~ as `format-check.py` - now we can run it on Windows - most people on this team know Python better than Bash - add `-i` option to edit files in place - the script runs the specific clang-format version via pipx, so even if you have some other version of clang-format installed, the script will get it right - CI just runs `./format-check.py` instead of using some 3rdparty Github Action --- .github/workflows/clang-format.yml | 11 +++--- format-check.py | 47 +++++++++++++++++++++++++ format-check.sh | 24 ------------- include/aws/common/atomics.h | 6 ++-- include/aws/common/byte_buf.h | 2 +- include/aws/common/condition_variable.h | 6 ++-- include/aws/common/error.h | 7 ++-- include/aws/common/logging.h | 4 +-- include/aws/common/macros.h | 2 +- include/aws/common/mutex.h | 6 ++-- include/aws/common/rw_lock.h | 6 ++-- include/aws/common/statistics.h | 2 +- include/aws/common/thread.h | 3 +- source/allocator.c | 2 +- source/android/logging.c | 4 ++- source/common.c | 2 +- source/posix/clock.c | 2 +- source/priority_queue.c | 2 +- tests/assert_test.c | 2 ++ tests/condition_variable_test.c | 32 +++++++++-------- tests/error_test.c | 16 +++++---- 21 files changed, 106 insertions(+), 82 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 d914a0955..36388d688 100644 --- a/.github/workflows/clang-format.yml +++ b/.github/workflows/clang-format.yml @@ -5,15 +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@v3 + 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,inl - source: 'source include tests verification' + 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 d292e9d7f..000000000 --- a/format-check.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env bash - -if [[ -z $CLANG_FORMAT ]] ; then - CLANG_FORMAT=clang-format -fi - -if ! type $CLANG_FORMAT 2> /dev/null ; then - echo "No appropriate clang-format found." - exit 1 -fi - -FAIL=0 -SOURCE_FILES=`find source include tests verification -type f \( -name '*.h' -o -name '*.c' -o -name '*.inl' \)` -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/common/atomics.h b/include/aws/common/atomics.h index ef64f985a..7f5445334 100644 --- a/include/aws/common/atomics.h +++ b/include/aws/common/atomics.h @@ -79,14 +79,12 @@ enum aws_memory_order { /** * Statically initializes an aws_atomic_var to a given size_t value. */ -#define AWS_ATOMIC_INIT_INT(x) \ - { .value = (void *)(uintptr_t)(x) } +#define AWS_ATOMIC_INIT_INT(x) {.value = (void *)(uintptr_t)(x)} /** * Statically initializes an aws_atomic_var to a given void * value. */ -#define AWS_ATOMIC_INIT_PTR(x) \ - { .value = (void *)(x) } +#define AWS_ATOMIC_INIT_PTR(x) {.value = (void *)(x)} AWS_EXTERN_C_BEGIN diff --git a/include/aws/common/byte_buf.h b/include/aws/common/byte_buf.h index 6fc5c3ff9..11efd3f78 100644 --- a/include/aws/common/byte_buf.h +++ b/include/aws/common/byte_buf.h @@ -61,7 +61,7 @@ struct aws_byte_cursor { * Helper Macro for initializing a byte cursor from a string literal */ #define AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(literal) \ - { .ptr = (uint8_t *)(const char *)(literal), .len = sizeof(literal) - 1 } + {.ptr = (uint8_t *)(const char *)(literal), .len = sizeof(literal) - 1} /** * Signature for function argument to trim APIs diff --git a/include/aws/common/condition_variable.h b/include/aws/common/condition_variable.h index ba5984cb8..82431dd4f 100644 --- a/include/aws/common/condition_variable.h +++ b/include/aws/common/condition_variable.h @@ -37,11 +37,9 @@ struct aws_condition_variable { * CONDITION_VARIABLE_INIT. */ #ifdef _WIN32 -# define AWS_CONDITION_VARIABLE_INIT \ - { .condition_handle = NULL, .initialized = true } +# define AWS_CONDITION_VARIABLE_INIT {.condition_handle = NULL, .initialized = true} #else -# define AWS_CONDITION_VARIABLE_INIT \ - { .condition_handle = PTHREAD_COND_INITIALIZER, .initialized = true } +# define AWS_CONDITION_VARIABLE_INIT {.condition_handle = PTHREAD_COND_INITIALIZER, .initialized = true} #endif AWS_EXTERN_C_BEGIN diff --git a/include/aws/common/error.h b/include/aws/common/error.h index a40b193fa..8c5d45f69 100644 --- a/include/aws/common/error.h +++ b/include/aws/common/error.h @@ -20,7 +20,7 @@ AWS_PUSH_SANE_WARNING_LEVEL /* Each library gets space for 2^^10 error entries */ #define AWS_ERROR_ENUM_STRIDE_BITS 10 #define AWS_ERROR_ENUM_STRIDE (1U << AWS_ERROR_ENUM_STRIDE_BITS) -#define AWS_ERROR_ENUM_BEGIN_RANGE(x) ((x)*AWS_ERROR_ENUM_STRIDE) +#define AWS_ERROR_ENUM_BEGIN_RANGE(x) ((x) * AWS_ERROR_ENUM_STRIDE) #define AWS_ERROR_ENUM_END_RANGE(x) (((x) + 1) * AWS_ERROR_ENUM_STRIDE - 1) struct aws_error_info { @@ -38,7 +38,10 @@ struct aws_error_info_list { #define AWS_DEFINE_ERROR_INFO(C, ES, LN) \ { \ - .literal_name = #C, .error_code = (C), .error_str = (ES), .lib_name = (LN), \ + .literal_name = #C, \ + .error_code = (C), \ + .error_str = (ES), \ + .lib_name = (LN), \ .formatted_name = LN ": " #C ", " ES, \ } diff --git a/include/aws/common/logging.h b/include/aws/common/logging.h index 009996475..f71a375b5 100644 --- a/include/aws/common/logging.h +++ b/include/aws/common/logging.h @@ -62,7 +62,7 @@ enum { AWS_LOG_SUBJECT_STRIDE_BITS = 10, }; #define AWS_LOG_SUBJECT_STRIDE (1U << AWS_LOG_SUBJECT_STRIDE_BITS) -#define AWS_LOG_SUBJECT_BEGIN_RANGE(x) ((x)*AWS_LOG_SUBJECT_STRIDE) +#define AWS_LOG_SUBJECT_BEGIN_RANGE(x) ((x) * AWS_LOG_SUBJECT_STRIDE) #define AWS_LOG_SUBJECT_END_RANGE(x) (((x) + 1) * AWS_LOG_SUBJECT_STRIDE - 1) struct aws_log_subject_info { @@ -72,7 +72,7 @@ struct aws_log_subject_info { }; #define DEFINE_LOG_SUBJECT_INFO(id, name, desc) \ - { .subject_id = (id), .subject_name = (name), .subject_description = (desc) } + {.subject_id = (id), .subject_name = (name), .subject_description = (desc)} struct aws_log_subject_info_list { struct aws_log_subject_info *subject_list; diff --git a/include/aws/common/macros.h b/include/aws/common/macros.h index da3298544..f5c64c6aa 100644 --- a/include/aws/common/macros.h +++ b/include/aws/common/macros.h @@ -160,6 +160,6 @@ enum { AWS_CACHE_LINE = 64 }; * this will get you back to the pointer of the object. member is the name of * the instance of struct aws_linked_list_node in your struct. */ -#define AWS_CONTAINER_OF(ptr, type, member) ((type *)((uint8_t *)(ptr)-offsetof(type, member))) +#define AWS_CONTAINER_OF(ptr, type, member) ((type *)((uint8_t *)(ptr) - offsetof(type, member))) #endif /* AWS_COMMON_MACROS_H */ diff --git a/include/aws/common/mutex.h b/include/aws/common/mutex.h index 3a491abc9..b59a58474 100644 --- a/include/aws/common/mutex.h +++ b/include/aws/common/mutex.h @@ -26,11 +26,9 @@ struct aws_mutex { }; #ifdef _WIN32 -# define AWS_MUTEX_INIT \ - { .mutex_handle = NULL, .initialized = true } +# define AWS_MUTEX_INIT {.mutex_handle = NULL, .initialized = true} #else -# define AWS_MUTEX_INIT \ - { .mutex_handle = PTHREAD_MUTEX_INITIALIZER, .initialized = true } +# define AWS_MUTEX_INIT {.mutex_handle = PTHREAD_MUTEX_INITIALIZER, .initialized = true} #endif AWS_EXTERN_C_BEGIN diff --git a/include/aws/common/rw_lock.h b/include/aws/common/rw_lock.h index 538841f63..3ee778537 100644 --- a/include/aws/common/rw_lock.h +++ b/include/aws/common/rw_lock.h @@ -25,11 +25,9 @@ struct aws_rw_lock { }; #ifdef _WIN32 -# define AWS_RW_LOCK_INIT \ - { .lock_handle = NULL } +# define AWS_RW_LOCK_INIT {.lock_handle = NULL} #else -# define AWS_RW_LOCK_INIT \ - { .lock_handle = PTHREAD_RWLOCK_INITIALIZER } +# define AWS_RW_LOCK_INIT {.lock_handle = PTHREAD_RWLOCK_INITIALIZER} #endif AWS_EXTERN_C_BEGIN diff --git a/include/aws/common/statistics.h b/include/aws/common/statistics.h index f9d432d33..efeed7228 100644 --- a/include/aws/common/statistics.h +++ b/include/aws/common/statistics.h @@ -23,7 +23,7 @@ enum { }; #define AWS_CRT_STATISTICS_CATEGORY_STRIDE (1U << AWS_CRT_STATISTICS_CATEGORY_STRIDE_BITS) -#define AWS_CRT_STATISTICS_CATEGORY_BEGIN_RANGE(x) ((x)*AWS_CRT_STATISTICS_CATEGORY_STRIDE) +#define AWS_CRT_STATISTICS_CATEGORY_BEGIN_RANGE(x) ((x) * AWS_CRT_STATISTICS_CATEGORY_STRIDE) #define AWS_CRT_STATISTICS_CATEGORY_END_RANGE(x) (((x) + 1) * AWS_CRT_STATISTICS_CATEGORY_STRIDE - 1) /** diff --git a/include/aws/common/thread.h b/include/aws/common/thread.h index 90f0bc363..84180e690 100644 --- a/include/aws/common/thread.h +++ b/include/aws/common/thread.h @@ -90,8 +90,7 @@ struct aws_thread_options { typedef union { void *ptr; } aws_thread_once; -# define AWS_THREAD_ONCE_STATIC_INIT \ - { NULL } +# define AWS_THREAD_ONCE_STATIC_INIT {NULL} typedef unsigned long aws_thread_id_t; #else typedef pthread_once_t aws_thread_once; diff --git a/source/allocator.c b/source/allocator.c index d3d1e98bd..71f7a64b0 100644 --- a/source/allocator.c +++ b/source/allocator.c @@ -207,7 +207,7 @@ void *aws_mem_calloc(struct aws_allocator *allocator, size_t num, size_t size) { return mem; } -#define AWS_ALIGN_ROUND_UP(value, alignment) (((value) + ((alignment)-1)) & ~((alignment)-1)) +#define AWS_ALIGN_ROUND_UP(value, alignment) (((value) + ((alignment) - 1)) & ~((alignment) - 1)) void *aws_mem_acquire_many(struct aws_allocator *allocator, size_t count, ...) { diff --git a/source/android/logging.c b/source/android/logging.c index 6c2d9de53..303bf36bf 100644 --- a/source/android/logging.c +++ b/source/android/logging.c @@ -107,7 +107,9 @@ static int s_logcat_format(struct logcat_format_data *formatting_data, va_list a return AWS_OP_SUCCESS; } -static struct aws_logger_logcat { enum aws_log_level level; } s_logcat_impl; +static struct aws_logger_logcat { + enum aws_log_level level; +} s_logcat_impl; static int s_logcat_log( struct aws_logger *logger, diff --git a/source/common.c b/source/common.c index 2c971b5b4..3bedda749 100644 --- a/source/common.c +++ b/source/common.c @@ -80,7 +80,7 @@ void aws_secure_zero(void *pBuf, size_t bufsize) { #endif // #else not windows } -#define AWS_DEFINE_ERROR_INFO_COMMON(C, ES) [(C)-0x0000] = AWS_DEFINE_ERROR_INFO(C, ES, "aws-c-common") +#define AWS_DEFINE_ERROR_INFO_COMMON(C, ES) [(C) - 0x0000] = AWS_DEFINE_ERROR_INFO(C, ES, "aws-c-common") /* clang-format off */ static struct aws_error_info errors[] = { AWS_DEFINE_ERROR_INFO_COMMON( diff --git a/source/posix/clock.c b/source/posix/clock.c index b2c3bc3f0..076f4d835 100644 --- a/source/posix/clock.c +++ b/source/posix/clock.c @@ -47,7 +47,7 @@ static int (*s_gettime_fn)(clockid_t clock_id, struct timespec *tp) = NULL; static void s_do_osx_loads(void *user_data) { (void)user_data; - s_gettime_fn = (int (*)(clockid_t clock_id, struct timespec * tp)) dlsym(RTLD_DEFAULT, "clock_gettime"); + s_gettime_fn = (int (*)(clockid_t clock_id, struct timespec *tp))dlsym(RTLD_DEFAULT, "clock_gettime"); } int aws_high_res_clock_get_ticks(uint64_t *timestamp) { diff --git a/source/priority_queue.c b/source/priority_queue.c index fcea718bf..2157ffd0a 100644 --- a/source/priority_queue.c +++ b/source/priority_queue.c @@ -7,7 +7,7 @@ #include -#define PARENT_OF(index) (((index)&1) ? (index) >> 1 : (index) > 1 ? ((index)-2) >> 1 : 0) +#define PARENT_OF(index) (((index) & 1) ? (index) >> 1 : (index) > 1 ? ((index) - 2) >> 1 : 0) #define LEFT_OF(index) (((index) << 1) + 1) #define RIGHT_OF(index) (((index) << 1) + 2) diff --git a/tests/assert_test.c b/tests/assert_test.c index 52a4794f5..430a3f9e4 100644 --- a/tests/assert_test.c +++ b/tests/assert_test.c @@ -65,6 +65,7 @@ int side_effect(void) { } /* NOLINTNEXTLINE(readability-function-size) */ +/* clang-format off */ int test_asserts(int *index) { TEST_SUCCESS(null_test) {} TEST_FAILURE(null_failure_test) { @@ -160,6 +161,7 @@ int test_asserts(int *index) { return NO_MORE_TESTS; } +/* clang-format on */ void reset(void) { g_cur_testname = "UNKNOWN"; diff --git a/tests/condition_variable_test.c b/tests/condition_variable_test.c index e7ddd5f25..a59e7c2ba 100644 --- a/tests/condition_variable_test.c +++ b/tests/condition_variable_test.c @@ -65,13 +65,15 @@ static int s_test_conditional_notify_one_fn(struct aws_allocator *allocator, voi struct condition_predicate_args predicate_args = {.call_count = 0}; - struct conditional_test_data test_data = {.condition_variable_1 = AWS_CONDITION_VARIABLE_INIT, - .condition_variable_2 = AWS_CONDITION_VARIABLE_INIT, - .mutex = AWS_MUTEX_INIT, - .predicate_args = &predicate_args, - .thread_1 = 0, - .thread_2 = 0, - .thread_3 = 0}; + struct conditional_test_data test_data = { + .condition_variable_1 = AWS_CONDITION_VARIABLE_INIT, + .condition_variable_2 = AWS_CONDITION_VARIABLE_INIT, + .mutex = AWS_MUTEX_INIT, + .predicate_args = &predicate_args, + .thread_1 = 0, + .thread_2 = 0, + .thread_3 = 0, + }; ASSERT_SUCCESS(aws_mutex_lock(&test_data.mutex)); @@ -103,13 +105,15 @@ static int s_test_conditional_notify_all_fn(struct aws_allocator *allocator, voi struct condition_predicate_args predicate_args = {.call_count = 0}; - struct conditional_test_data test_data = {.condition_variable_1 = AWS_CONDITION_VARIABLE_INIT, - .condition_variable_2 = AWS_CONDITION_VARIABLE_INIT, - .mutex = AWS_MUTEX_INIT, - .predicate_args = &predicate_args, - .thread_1 = 0, - .thread_2 = 0, - .thread_3 = 0}; + struct conditional_test_data test_data = { + .condition_variable_1 = AWS_CONDITION_VARIABLE_INIT, + .condition_variable_2 = AWS_CONDITION_VARIABLE_INIT, + .mutex = AWS_MUTEX_INIT, + .predicate_args = &predicate_args, + .thread_1 = 0, + .thread_2 = 0, + .thread_3 = 0, + }; ASSERT_SUCCESS(aws_mutex_lock(&test_data.mutex)); diff --git a/tests/error_test.c b/tests/error_test.c index b7a241ec8..409dc5a34 100644 --- a/tests/error_test.c +++ b/tests/error_test.c @@ -399,13 +399,15 @@ static void s_error_thread_fn(void *arg) { static int s_error_code_cross_thread_test_fn(struct aws_allocator *allocator, void *ctx) { (void)ctx; - struct error_thread_test_data test_data = {.thread_1_code = 0, - .thread_1_get_last_code = 0, - .thread_1_encountered_count = 0, - .thread_2_code = 0, - .thread_2_get_last_code = 0, - .thread_2_encountered_count = 0, - .thread_2_id = 0}; + struct error_thread_test_data test_data = { + .thread_1_code = 0, + .thread_1_get_last_code = 0, + .thread_1_encountered_count = 0, + .thread_2_code = 0, + .thread_2_get_last_code = 0, + .thread_2_encountered_count = 0, + .thread_2_id = 0, + }; test_data.thread_1_id = aws_thread_current_thread_id();