Skip to content

Commit

Permalink
Clang14 tidy (#1048)
Browse files Browse the repository at this point in the history
  • Loading branch information
TingDaoK authored Jul 28, 2023
1 parent 657d921 commit 0f7f946
Show file tree
Hide file tree
Showing 30 changed files with 90 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
Checks: 'clang-diagnostic-*,clang-analyzer-*,readability-*,modernize-*,bugprone-*,misc-*,google-runtime-int,llvm-header-guard,fuchsia-restrict-system-includes,-clang-analyzer-valist.Uninitialized,-clang-analyzer-security.insecureAPI.rand,-clang-analyzer-alpha.*,-readability-magic-numbers,-readability-non-const-parameter,-readability-avoid-const-params-in-decls,-readability-else-after-return,-readability-isolate-declaration,-readability-uppercase-literal-suffix,-bugprone-sizeof-expression'
Checks: 'clang-diagnostic-*,clang-analyzer-*,readability-*,modernize-*,bugprone-*,misc-*,google-runtime-int,llvm-header-guard,fuchsia-restrict-system-includes,-clang-analyzer-valist.Uninitialized,-clang-analyzer-security.insecureAPI.rand,-clang-analyzer-alpha.*,-readability-magic-numbers,-readability-non-const-parameter,-readability-avoid-const-params-in-decls,-readability-else-after-return,-readability-isolate-declaration,-readability-uppercase-literal-suffix,-bugprone-sizeof-expression,-bugprone-easily-swappable-parameters,-readability-identifier-length,-misc-no-recursion,-readability-function-cognitive-complexity,-readability-magic-numbers'
WarningsAsErrors: '*'
HeaderFilterRegex: '.*\.[h|inl]$'
FormatStyle: 'file'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/clang-tidy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:

strategy:
matrix:
host: [ubuntu-20.04] # latest
host: [ubuntu-22.04] # latest

runs-on: ${{ matrix.host }}

Expand Down
2 changes: 1 addition & 1 deletion include/aws/common/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct aws_allocator *aws_default_allocator(void);

#ifdef __MACH__
/* Avoid pulling in CoreFoundation headers in a header file. */
struct __CFAllocator;
struct __CFAllocator; /* NOLINT(bugprone-reserved-identifier) */
typedef const struct __CFAllocator *CFAllocatorRef;

/**
Expand Down
2 changes: 1 addition & 1 deletion include/aws/common/array_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

AWS_PUSH_SANE_WARNING_LEVEL

#define AWS_ARRAY_LIST_DEBUG_FILL 0xDD
enum { AWS_ARRAY_LIST_DEBUG_FILL = 0xDD };

struct aws_array_list {
struct aws_allocator *alloc;
Expand Down
6 changes: 4 additions & 2 deletions include/aws/common/date_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

AWS_PUSH_SANE_WARNING_LEVEL

#define AWS_DATE_TIME_STR_MAX_LEN 100
#define AWS_DATE_TIME_STR_MAX_BASIC_LEN 20
enum {
AWS_DATE_TIME_STR_MAX_LEN = 100,
AWS_DATE_TIME_STR_MAX_BASIC_LEN = 20,
};

struct aws_byte_buf;
struct aws_byte_cursor;
Expand Down
8 changes: 5 additions & 3 deletions include/aws/common/hash_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@

AWS_PUSH_SANE_WARNING_LEVEL

#define AWS_COMMON_HASH_TABLE_ITER_CONTINUE (1 << 0)
#define AWS_COMMON_HASH_TABLE_ITER_DELETE (1 << 1)
#define AWS_COMMON_HASH_TABLE_ITER_ERROR (1 << 2)
enum {
AWS_COMMON_HASH_TABLE_ITER_CONTINUE = (1 << 0),
AWS_COMMON_HASH_TABLE_ITER_DELETE = (1 << 1),
AWS_COMMON_HASH_TABLE_ITER_ERROR = (1 << 2),
};

/**
* Hash table data structure. This module provides an automatically resizing
Expand Down
4 changes: 3 additions & 1 deletion include/aws/common/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ enum aws_log_level {
typedef uint32_t aws_log_subject_t;

/* Each library gets space for 2^^10 log subject entries */
#define AWS_LOG_SUBJECT_STRIDE_BITS 10
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_END_RANGE(x) (((x) + 1) * AWS_LOG_SUBJECT_STRIDE - 1)
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 @@ -61,7 +61,7 @@ AWS_STATIC_ASSERT(CALL_OVERLOAD_TEST(1) == 1);
AWS_STATIC_ASSERT(CALL_OVERLOAD_TEST(1, 2) == 2);
AWS_STATIC_ASSERT(CALL_OVERLOAD_TEST(1, 2, 3) == 3);

#define AWS_CACHE_LINE 64
enum { AWS_CACHE_LINE = 64 };
/**
* Format macro for strings of a specified length.
* Allows non null-terminated strings to be used with the printf family of functions.
Expand Down
5 changes: 4 additions & 1 deletion include/aws/common/statistics.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ struct aws_array_list;
typedef uint32_t aws_crt_statistics_category_t;

/* Each library gets space for 2^^8 category entries */
#define AWS_CRT_STATISTICS_CATEGORY_STRIDE_BITS 8
enum {
AWS_CRT_STATISTICS_CATEGORY_STRIDE_BITS = 8,
};

#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_END_RANGE(x) (((x) + 1) * AWS_CRT_STATISTICS_CATEGORY_STRIDE - 1)
Expand Down
2 changes: 1 addition & 1 deletion include/aws/common/uuid.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct aws_uuid {
};

/* 36 bytes for the UUID plus one more for the null terminator. */
#define AWS_UUID_STR_LEN 37
enum { AWS_UUID_STR_LEN = 37 };

AWS_EXTERN_C_BEGIN

Expand Down
8 changes: 4 additions & 4 deletions source/allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static void *s_default_malloc(struct aws_allocator *allocator, size_t size) {
* We use PAGE_SIZE as the boundary because we are not aware of any allocations of
* this size or greater that are not data buffers
*/
const size_t alignment = sizeof(void *) * (size > PAGE_SIZE ? 8 : 2);
const size_t alignment = sizeof(void *) * (size > (size_t)PAGE_SIZE ? 8 : 2);
#if !defined(_WIN32)
void *result = NULL;
int err = posix_memalign(&result, alignment, size);
Expand Down Expand Up @@ -92,7 +92,7 @@ static void *s_default_realloc(struct aws_allocator *allocator, void *ptr, size_

return new_mem;
#else
const size_t alignment = sizeof(void *) * (newsize > PAGE_SIZE ? 8 : 2);
const size_t alignment = sizeof(void *) * (newsize > (size_t)PAGE_SIZE ? 8 : 2);
void *new_mem = _aligned_realloc(ptr, newsize, alignment);
AWS_PANIC_OOM(new_mem, "Unhandled OOM encountered in _aligned_realloc");
return new_mem;
Expand Down Expand Up @@ -289,7 +289,7 @@ static void *s_cf_allocator_reallocate(void *ptr, CFIndex new_size, CFOptionFlag
memcpy(&original_size, original_allocation, sizeof(size_t));

aws_mem_realloc(allocator, &original_allocation, original_size, (size_t)new_size);

AWS_FATAL_ASSERT(original_allocation);
size_t new_allocation_size = (size_t)new_size;
memcpy(original_allocation, &new_allocation_size, sizeof(size_t));

Expand All @@ -306,7 +306,7 @@ static CFIndex s_cf_allocator_preferred_size(CFIndex size, CFOptionFlags hint, v
(void)hint;
(void)info;

return size + sizeof(size_t);
return (CFIndex)(size + sizeof(size_t));
}

CFAllocatorRef aws_wrapped_cf_allocator_new(struct aws_allocator *allocator) {
Expand Down
4 changes: 2 additions & 2 deletions source/allocator_sba.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#define AWS_SBA_TAG_VALUE 0x736f6d6570736575ULL

/* list of sizes of bins, must be powers of 2, and less than AWS_SBA_PAGE_SIZE * 0.5 */
#define AWS_SBA_BIN_COUNT 5
enum { AWS_SBA_BIN_COUNT = 5 };
static const size_t s_bin_sizes[AWS_SBA_BIN_COUNT] = {32, 64, 128, 256, 512};
static const size_t s_max_bin_size = 512;

Expand Down Expand Up @@ -348,7 +348,7 @@ static void s_sba_free_to_bin(struct sba_bin *bin, void *addr) {
uint8_t *page_start = (uint8_t *)page + sizeof(struct page_header);
uint8_t *page_end = page_start + AWS_SBA_PAGE_SIZE;
/* Remove all chunks in the page from the free list */
intptr_t chunk_idx = bin->free_chunks.length;
intptr_t chunk_idx = (intptr_t)bin->free_chunks.length;
for (; chunk_idx >= 0; --chunk_idx) {
uint8_t *chunk = NULL;
aws_array_list_get_at(&bin->free_chunks, &chunk, chunk_idx);
Expand Down
6 changes: 3 additions & 3 deletions source/date_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ static int s_parse_iso_8601_basic(const struct aws_byte_cursor *date_str_cursor,
AWS_ZERO_STRUCT(*parsed_time);

while (state < FINISHED && !error && index < date_str_cursor->len) {
char c = date_str_cursor->ptr[index];
char c = (char)date_str_cursor->ptr[index];
size_t sub_index = index - state_start_index;
switch (state) {
case ON_YEAR:
Expand Down Expand Up @@ -322,7 +322,7 @@ static int s_parse_iso_8601(const struct aws_byte_cursor *date_str_cursor, struc
AWS_ZERO_STRUCT(*parsed_time);

while (state < FINISHED && !error && index < date_str_cursor->len) {
char c = date_str_cursor->ptr[index];
char c = (char)date_str_cursor->ptr[index];
switch (state) {
case ON_YEAR:
if (c == '-' && index - state_start_index == 4) {
Expand Down Expand Up @@ -446,7 +446,7 @@ static int s_parse_rfc_822(
AWS_ZERO_STRUCT(*parsed_time);

while (!error && index < len) {
char c = date_str_cursor->ptr[index];
char c = (char)date_str_cursor->ptr[index];

switch (state) {
/* week day abbr is optional. */
Expand Down
2 changes: 1 addition & 1 deletion source/encoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ int aws_hex_decode(const struct aws_byte_cursor *AWS_RESTRICT to_decode, struct
/* if the buffer isn't even, prepend a 0 to the buffer. */
if (AWS_UNLIKELY(to_decode->len & 0x01)) {
i = 1;
if (s_hex_decode_char_to_int(to_decode->ptr[0], &low_value)) {
if (s_hex_decode_char_to_int((char)to_decode->ptr[0], &low_value)) {
return aws_raise_error(AWS_ERROR_INVALID_HEX_STR);
}

Expand Down
2 changes: 1 addition & 1 deletion source/hash_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ uint64_t aws_hash_byte_cursor_ptr(const void *item) {
/* first digits of pi in hex */
uint32_t b = 0x3243F6A8, c = 0x885A308D;
hashlittle2(cur->ptr, cur->len, &c, &b);
AWS_RETURN_WITH_POSTCONDITION(((uint64_t)b << 32) | c, aws_byte_cursor_is_valid(cur));
AWS_RETURN_WITH_POSTCONDITION(((uint64_t)b << 32) | c, aws_byte_cursor_is_valid(cur)); /* NOLINT */
}

uint64_t aws_hash_ptr(const void *item) {
Expand Down
16 changes: 9 additions & 7 deletions source/log_formatter.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@
# pragma warning(disable : 4204) /* non-constant aggregate initializer */
#endif

/* (max) strlen of "[<LogLevel>]" */
#define LOG_LEVEL_PREFIX_PADDING 7
enum {
/* (max) strlen of "[<LogLevel>]" */
LOG_LEVEL_PREFIX_PADDING = 7,

/* (max) strlen of "[<ThreadId>]" */
#define THREAD_ID_PREFIX_PADDING 22
/* (max) strlen of "[<ThreadId>]" */
THREAD_ID_PREFIX_PADDING = 22,

/* strlen of (user-content separator) " - " + "\n" + spaces between prefix fields + brackets around timestamp + 1 +
subject_name padding */
#define MISC_PADDING 15
/* strlen of (user-content separator) " - " + "\n" + spaces between prefix fields + brackets around timestamp + 1 +
subject_name padding */
MISC_PADDING = 15,
};

#define MAX_LOG_LINE_PREFIX_SIZE \
(LOG_LEVEL_PREFIX_PADDING + THREAD_ID_PREFIX_PADDING + MISC_PADDING + AWS_DATE_TIME_STR_MAX_LEN)
Expand Down
2 changes: 1 addition & 1 deletion source/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ static enum aws_log_level s_noalloc_stderr_logger_get_log_level(struct aws_logge
return (enum aws_log_level)aws_atomic_load_int(&impl->level);
}

#define MAXIMUM_NO_ALLOC_LOG_LINE_SIZE 8192
enum { MAXIMUM_NO_ALLOC_LOG_LINE_SIZE = 8192 };

static int s_noalloc_stderr_logger_log(
struct aws_logger *logger,
Expand Down
2 changes: 1 addition & 1 deletion source/memtrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct alloc_tracer {
};

/* number of frames to skip in call stacks (s_alloc_tracer_track, and the vtable function) */
#define FRAMES_TO_SKIP 2
enum { FRAMES_TO_SKIP = 2 };

static void *s_trace_mem_acquire(struct aws_allocator *allocator, size_t size);
static void s_trace_mem_release(struct aws_allocator *allocator, void *ptr);
Expand Down
4 changes: 2 additions & 2 deletions source/posix/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ static int s_legacy_get_time(uint64_t *timestamp) {

# if MAC_OS_X_VERSION_MAX_ALLOWED >= 101200
static aws_thread_once s_thread_once_flag = AWS_THREAD_ONCE_STATIC_INIT;
static int (*s_gettime_fn)(clockid_t __clock_id, struct timespec *__tp) = NULL;
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
6 changes: 2 additions & 4 deletions source/posix/condition_variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,10 @@ int aws_condition_variable_wait_for(
return AWS_OP_ERR;
}

time_to_wait += current_sys_time;

struct timespec ts;
uint64_t remainder = 0;
ts.tv_sec =
(time_t)aws_timestamp_convert((uint64_t)time_to_wait, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, &remainder);
ts.tv_sec = (time_t)aws_timestamp_convert(
(uint64_t)(time_to_wait + current_sys_time), AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, &remainder);
ts.tv_nsec = (long)remainder;

int err_code = pthread_cond_timedwait(&condition_variable->condition_handle, &mutex->mutex_handle, &ts);
Expand Down
3 changes: 2 additions & 1 deletion source/posix/device_random.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ int aws_device_random_buffer_append(struct aws_byte_buf *output, size_t n) {
/* read() can fail if N is too large (e.g. x64 macos fails if N > INT32_MAX),
* so work in reasonably sized chunks. */
while (n > 0) {
size_t capped_n = aws_min_size(n, 1024 * 1024 * 1024 * 1 /* 1GiB */);
size_t capped_n = aws_min_size(
n, 1024 * 1024 * 1024 * 1 /* 1GiB */); /* NOLINT(bugprone-implicit-widening-of-multiplication-result) */

ssize_t amount_read = read(s_rand_fd, output->buffer + output->len, capped_n);

Expand Down
8 changes: 5 additions & 3 deletions source/posix/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ int aws_mutex_init(struct aws_mutex *mutex) {
int return_code = AWS_OP_SUCCESS;

if (!err_code) {
if ((err_code = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL)) ||
(err_code = pthread_mutex_init(&mutex->mutex_handle, &attr))) {

err_code = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
if (!err_code) {
err_code = pthread_mutex_init(&mutex->mutex_handle, &attr);
}
if (err_code) {
return_code = aws_private_convert_and_raise_error_code(err_code);
}
pthread_mutexattr_destroy(&attr);
Expand Down
4 changes: 2 additions & 2 deletions source/posix/system_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ uint16_t aws_get_cpu_group_count(void) {
return (uint16_t)g_numa_num_configured_nodes_ptr();
}

return 1u;
return 1U;
}

size_t aws_get_cpu_count_for_group(uint16_t group_idx) {
Expand Down Expand Up @@ -242,7 +242,7 @@ int s_parse_symbol(const char *symbol, void *addr, struct aws_stack_frame_info *
if (function_len >= (sizeof(frame->function) - 1)) {
function_len = sizeof(frame->function) - 1;
}
strncpy(frame->function, function_start, function_end - function_start);
strncpy(frame->function, function_start, function_len);

/* find base addr for library/exe */
Dl_info addr_info;
Expand Down
2 changes: 1 addition & 1 deletion source/posix/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

#if !defined(__MACH__)
# define _GNU_SOURCE
# define _GNU_SOURCE /* NOLINT(bugprone-reserved-identifier) */
#endif

#include <aws/common/clock.h>
Expand Down
2 changes: 1 addition & 1 deletion source/process_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <stdio.h>
#include <sys/types.h>

#define MAX_BUFFER_SIZE (2048)
enum { MAX_BUFFER_SIZE = 2048 };

int aws_run_command_result_init(struct aws_allocator *allocator, struct aws_run_command_result *result) {
if (!allocator || !result) {
Expand Down
6 changes: 3 additions & 3 deletions source/thread_scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct cancellation_node {

static void s_destroy_callback(void *arg) {
struct aws_thread_scheduler *scheduler = arg;
aws_atomic_store_int(&scheduler->should_exit, 1u);
aws_atomic_store_int(&scheduler->should_exit, 1U);
aws_condition_variable_notify_all(&scheduler->thread_data.c_var);
aws_thread_join(&scheduler->thread);
aws_task_scheduler_clean_up(&scheduler->scheduler);
Expand Down Expand Up @@ -138,7 +138,7 @@ struct aws_thread_scheduler *aws_thread_scheduler_new(
}

scheduler->allocator = allocator;
aws_atomic_init_int(&scheduler->should_exit, 0u);
aws_atomic_init_int(&scheduler->should_exit, 0U);
aws_ref_count_init(&scheduler->ref_count, scheduler, s_destroy_callback);
aws_linked_list_init(&scheduler->thread_data.scheduling_queue);
aws_linked_list_init(&scheduler->thread_data.cancel_queue);
Expand Down Expand Up @@ -182,7 +182,7 @@ void aws_thread_scheduler_schedule_future(
aws_condition_variable_notify_one(&scheduler->thread_data.c_var);
}
void aws_thread_scheduler_schedule_now(struct aws_thread_scheduler *scheduler, struct aws_task *task) {
aws_thread_scheduler_schedule_future(scheduler, task, 0u);
aws_thread_scheduler_schedule_future(scheduler, task, 0U);
}

void aws_thread_scheduler_cancel_task(struct aws_thread_scheduler *scheduler, struct aws_task *task) {
Expand Down
2 changes: 1 addition & 1 deletion source/thread_shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ int aws_thread_join_all_managed(void) {
aws_condition_variable_wait_for_pred(
&s_managed_thread_signal,
&s_managed_thread_lock,
wait_ns,
(int64_t)wait_ns,
s_one_or_fewer_managed_threads_unjoined,
NULL);
} else {
Expand Down
2 changes: 1 addition & 1 deletion source/windows/system_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ size_t aws_system_info_processor_count(void) {
/* the next three functions need actual implementations before we can have proper numa alignment on windows.
* For now leave them stubbed out. */
uint16_t aws_get_cpu_group_count(void) {
return 1u;
return 1U;
}

size_t aws_get_cpu_count_for_group(uint16_t group_idx) {
Expand Down
Loading

0 comments on commit 0f7f946

Please sign in to comment.