Skip to content

Commit

Permalink
clang-format 18 (#1113)
Browse files Browse the repository at this point in the history
**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
  • Loading branch information
graebm authored Jun 12, 2024
1 parent 4f874ce commit 4fccc79
Show file tree
Hide file tree
Showing 21 changed files with 106 additions and 82 deletions.
11 changes: 4 additions & 7 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
with:
# List of extensions to check
extensions: c,h,inl
source: 'source include tests verification'
run: |
./format-check.py
47 changes: 47 additions & 0 deletions format-check.py
Original file line number Diff line number Diff line change
@@ -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)
24 changes: 0 additions & 24 deletions format-check.sh

This file was deleted.

6 changes: 2 additions & 4 deletions include/aws/common/atomics.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion include/aws/common/byte_buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions include/aws/common/condition_variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions include/aws/common/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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, \
}

Expand Down
4 changes: 2 additions & 2 deletions include/aws/common/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion include/aws/common/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
6 changes: 2 additions & 4 deletions include/aws/common/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions include/aws/common/rw_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion include/aws/common/statistics.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

/**
Expand Down
3 changes: 1 addition & 2 deletions include/aws/common/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion source/allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...) {

Expand Down
4 changes: 3 additions & 1 deletion source/android/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion source/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion source/posix/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion source/priority_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <string.h>

#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)

Expand Down
2 changes: 2 additions & 0 deletions tests/assert_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -160,6 +161,7 @@ int test_asserts(int *index) {

return NO_MORE_TESTS;
}
/* clang-format on */

void reset(void) {
g_cur_testname = "UNKNOWN";
Expand Down
32 changes: 18 additions & 14 deletions tests/condition_variable_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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));

Expand Down
16 changes: 9 additions & 7 deletions tests/error_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 4fccc79

Please sign in to comment.