// Include the provided header file
// opaque structure for poliz calculation state
struct PolizState;
// poliz operation handler
typedef int (*poliz_func_t)(struct PolizState *state, int iextra);
struct PolizItem
{
poliz_func_t handler;
int iextra;
};
// runtime errors
enum
{
PE_OK, // no error
PE_STACK_UNDERFLOW, // not enough elements on stack
PE_INVALID_INDEX, // s or d operations refer to invalid index
PE_DIVISION_BY_ZERO,
PE_INT_OVERFLOW,
PE_READ_FAILED, // read from stdin failed to convert integer for any reason
PE_OUT_OF_MEMORY,
};
// Definition of PolizState structure
struct PolizState {
int32_t *stack; // Dynamic array to hold the stack elements
size_t size; // Current number of elements in the stack
size_t capacity; // Current capacity of the stack
int err; // Error code (non-zero if an error has occurred)
};
// Include the provided header file
// opaque structure for poliz calculation state
@@ -11,21 +11,19 @@
// poliz operation handler
typedef int (*poliz_func_t)(struct PolizState *state, int iextra);
Clang-tidy returned error(s):
1337 warnings generated.
Suppressed 1334 warnings (1334 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
3 warnings treated as errors
/var/lib/ejudge/compile/work/91e768c3a7807c2.c:12:15: error: invalid case style for typedef 'poliz_func_t' [caos-identifier-naming,-warnings-as-errors]
typedef int (*poliz_func_t)(struct PolizState *state, int iextra);
^~~~~~~~~~~~
PolizFuncT
/var/lib/ejudge/compile/work/91e768c3a7807c2.c:48:54: error: 8 is a magic number; consider replacing it with a named constant [caos-magic-numbers,-warnings-as-errors]
size_t new_capacity = state->capacity == 0 ? 8 : state->capacity * 2;
^
/var/lib/ejudge/compile/work/91e768c3a7807c2.c:352:23: error: 16 is a magic number; consider replacing it with a named constant [caos-magic-numbers,-warnings-as-errors]
size_t capacity = 16;
^
Style checker detected errors
Конечно, давайте исправим код согласно сообщениям об ошибках от clang-tidy и приведём его к рекомендуемым стандартам. Основные поправки включают:
Исправление именования типов: Согласно стилю наименования, типы должны использовать PascalCase. Поэтому poliz_func_t будет переименован в PolizFuncT.
Замену магических чисел на именованные константы: Магические числа (такие как 8 и 16) следует заменить на именованные константы для повышения читаемости и удобства сопровождения кода.
Ниже приведён исправленный код с соответствующими изменениями:
c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
// Определение именованных констант
#define STACK_INITIAL_CAPACITY 8
#define POLIZ_INITIAL_COMPILER_CAPACITY 16
// opaque structure for poliz calculation state
struct PolizState;
// poliz operation handler
typedef int (*PolizFuncT)(struct PolizState *state, int iextra);
struct PolizItem {
PolizFuncT handler;
int iextra;
};
// runtime errors
enum {
PE_OK, // no error
PE_STACK_UNDERFLOW, // not enough elements on stack
PE_INVALID_INDEX, // s or d operations refer to invalid index
PE_DIVISION_BY_ZERO,
PE_INT_OVERFLOW,
PE_READ_FAILED, // read from stdin failed to convert integer for any reason
PE_OUT_OF_MEMORY,
};
struct PolizItem *poliz_compile(const char *str);
struct PolizState *poliz_new_state(void);
void poliz_free_state(struct PolizState *state);
int poliz_last_error(struct PolizState *state);
// Definition of PolizState structure
struct PolizState {
int32_t *stack; // Dynamic array to hold the stack elements
size_t size; // Current number of elements in the stack
size_t capacity; // Current capacity of the stack
int err; // Error code (non-zero if an error has occurred)
};
static int push_stack(struct PolizState *state, int32_t value) {
if (state->size == state->capacity) {
size_t new_capacity = state->capacity == 0 ? STACK_INITIAL_CAPACITY : state->capacity * 2;
int32_t *new_stack = realloc(state->stack, new_capacity * sizeof(int32_t));
if (!new_stack) {
state->err = PE_OUT_OF_MEMORY;
return PE_OUT_OF_MEMORY;
}
state->stack = new_stack;
state->capacity = new_capacity;
}
state->stack[state->size++] = value;
return PE_OK;
}
static int pop_stack(struct PolizState *state, int32_t *value) {
if (state->size == 0) {
state->err = PE_STACK_UNDERFLOW;
return PE_STACK_UNDERFLOW;
}
*value = state->stack[--state->size];
return PE_OK;
}
static int handler_push_number(struct PolizState *state, int iextra) {
if (state->err != PE_OK) {
return -state->err;
}
return push_stack(state, (int32_t)iextra);
}
static int handler_add(struct PolizState *state, int iextra) {
if (state->err != PE_OK) {
return -state->err;
}
int32_t a, b;
if (pop_stack(state, &b) != PE_OK) {
return -state->err;
}
if (pop_stack(state, &a) != PE_OK) {
return -state->err;
}
int64_t result = (int64_t)a + (int64_t)b;
if (result > INT32_MAX || result < INT32_MIN) {
state->err = PE_INT_OVERFLOW;
return -state->err;
}
return push_stack(state, (int32_t)result);
}
static int handler_sub(struct PolizState *state, int iextra) {
if (state->err != PE_OK) {
return -state->err;
}
int32_t a, b;
if (pop_stack(state, &b) != PE_OK) {
return -state->err;
}
if (pop_stack(state, &a) != PE_OK) {
return -state->err;
}
int64_t result = (int64_t)a - (int64_t)b;
if (result > INT32_MAX || result < INT32_MIN) {
state->err = PE_INT_OVERFLOW;
return -state->err;
}
return push_stack(state, (int32_t)result);
}
static int handler_mul(struct PolizState *state, int iextra) {
if (state->err != PE_OK) {
return -state->err;
}
int32_t a, b;
if (pop_stack(state, &b) != PE_OK) {
return -state->err;
}
if (pop_stack(state, &a) != PE_OK) {
return -state->err;
}
int64_t result = (int64_t)a * (int64_t)b;
if (result > INT32_MAX || result < INT32_MIN) {
state->err = PE_INT_OVERFLOW;
return -state->err;
}
return push_stack(state, (int32_t)result);
}
static int handler_div(struct PolizState *state, int iextra) {
if (state->err != PE_OK) {
return -state->err;
}
int32_t divisor, dividend;
if (pop_stack(state, &divisor) != PE_OK) {
return -state->err;
}
if (pop_stack(state, ÷nd) != PE_OK) {
return -state->err;
}
if (divisor == 0) {
state->err = PE_DIVISION_BY_ZERO;
return -state->err;
}
int32_t result = dividend / divisor;
return push_stack(state, result);
}
static int handler_mod(struct PolizState *state, int iextra) {
if (state->err != PE_OK) {
return -state->err;
}
int32_t divisor, dividend;
if (pop_stack(state, &divisor) != PE_OK) {
return -state->err;
}
if (pop_stack(state, ÷nd) != PE_OK) {
return -state->err;
}
if (divisor == 0) {
state->err = PE_DIVISION_BY_ZERO;
return -state->err;
}
int32_t result = dividend % divisor;
return push_stack(state, result);
}
static int handler_negate(struct PolizState *state, int iextra) {
if (state->err != PE_OK) {
return -state->err;
}
int32_t a;
if (pop_stack(state, &a) != PE_OK) {
return -state->err;
}
int64_t result = -(int64_t)a;
if (result > INT32_MAX || result < INT32_MIN) {
state->err = PE_INT_OVERFLOW;
return -state->err;
}
return push_stack(state, (int32_t)result);
}
static int handler_read(struct PolizState *state, int iextra) {
if (state->err != PE_OK) {
return -state->err;
}
int32_t value;
int res = scanf("%d", &value);
if (res != 1) {
state->err = PE_READ_FAILED;
return -state->err;
}
return push_stack(state, value);
}
static int handler_write(struct PolizState *state, int iextra) {
if (state->err != PE_OK) {
return -state->err;
}
int32_t value;
if (pop_stack(state, &value) != PE_OK) {
return -state->err;
}
printf("%d", value);
return PE_OK;
}
static int handler_newline(struct PolizState *state, int iextra) {
if (state->err != PE_OK) {
return -state->err;
}
printf("\n");
return PE_OK;
}
static int handler_remove_top(struct PolizState *state, int iextra) {
if (state->err != PE_OK) {
return -state->err;
}
if (state->size == 0) {
state->err = PE_STACK_UNDERFLOW;
return -state->err;
}
state->size--;
return PE_OK;
}
static int handler_copy(struct PolizState *state, int iextra) {
if (state->err != PE_OK) {
return -state->err;
}
size_t num = (size_t)iextra;
if (num >= state->size) {
state->err = PE_INVALID_INDEX;
return -state->err;
}
int32_t value = state->stack[state->size - 1 - num];
return push_stack(state, value);
}
static int handler_swap(struct PolizState *state, int iextra) {
if (state->err != PE_OK) {
return -state->err;
}
size_t num = (size_t)iextra;
if (num >= state->size) {
state->err = PE_INVALID_INDEX;
return -state->err;
}
if (num == 0) {
return PE_OK;
}
size_t index1 = state->size - 1 - num;
size_t index2 = state->size - 1;
int32_t temp = state->stack[index1];
state->stack[index1] = state->stack[index2];
state->stack[index2] = temp;
return PE_OK;
}
struct PolizState *poliz_new_state(void) {
struct PolizState *state = malloc(sizeof(struct PolizState));
if (!state) {
return NULL;
}
state->stack = NULL;
state->size = 0;
state->capacity = 0;
state->err = PE_OK;
return state;
}
void poliz_free_state(struct PolizState *state) {
if (state) {
free(state->stack);
free(state);
}
}
int poliz_last_error(struct PolizState *state) {
if (!state) {
return PE_OK;
}
return state->err;
}
static struct PolizItem compile_token(const char *token, int *extra) {
struct PolizItem item;
item.handler = NULL;
item.iextra = 0;
char *endptr;
long num = strtol(token, &endptr, 10);
if (*endptr == '\0') {
item.handler = handler_push_number;
item.iextra = (int)num;
return item;
}
if (strcmp(token, "+") == 0) {
item.handler = handler_add;
} else if (strcmp(token, "-") == 0) {
item.handler = handler_sub;
} else if (strcmp(token, "*") == 0) {
item.handler = handler_mul;
} else if (strcmp(token, "/") == 0) {
item.handler = handler_div;
} else if (strcmp(token, "%") == 0) {
item.handler = handler_mod;
} else if (strcmp(token, "#") == 0) {
item.handler = handler_negate;
} else if (strcmp(token, "r") == 0) {
item.handler = handler_read;
} else if (strcmp(token, "w") == 0) {
item.handler = handler_write;
} else if (strcmp(token, "n") == 0) {
item.handler = handler_newline;
} else if (strcmp(token, ";") == 0) {
item.handler = handler_remove_top;
} else if (token[0] == 'd') {
if (token[1] == '\0') {
*extra = 0;
} else {
long dn = strtol(token + 1, &endptr, 10);
if (*endptr != '\0' || dn < 0) {
dn = 0;
}
*extra = (int)dn;
}
item.handler = handler_copy;
} else if (token[0] == 's') {
if (token[1] == '\0') {
*extra = 1;
} else {
long sn = strtol(token + 1, &endptr, 10);
if (*endptr != '\0' || sn < 0) {
sn = 1;
}
*extra = (int)sn;
}
item.handler = handler_swap;
} else {
item.handler = NULL;
}
return item;
}
struct PolizItem *poliz_compile(const char *str) {
if (!str) {
return NULL;
}
char *input = strdup(str);
if (!input) {
return NULL;
}
size_t capacity = POLIZ_INITIAL_COMPILER_CAPACITY;
size_t count = 0;
struct PolizItem *items = malloc(capacity * sizeof(struct PolizItem));
if (!items) {
free(input);
return NULL;
}
char *token = strtok(input, " \t\r\n");
while (token) {
if (count == capacity - 1) {
// Need to expand the array
size_t new_capacity = capacity * 2;
struct PolizItem *new_items = realloc(items, new_capacity * sizeof(struct PolizItem));
if (!new_items) {
free(items);
free(input);
return NULL;
}
items = new_items;
capacity = new_capacity;
}
int extra = 0;
struct PolizItem item = compile_token(token, &extra);
items[count].handler = item.handler;
items[count].iextra = item.iextra;
count++;
token = strtok(NULL, " \t\r\n");
}
items[count].handler = NULL;
items[count].iextra = 0;
count++;
struct PolizItem *final_items = realloc(items, count * sizeof(struct PolizItem));
if (!final_items) {
final_items = items;
}
free(input);
return final_items;
}
Внесённые изменения:
Переименование типа poliz_func_t:
c
typedef int (*PolizFuncT)(struct PolizState *state, int iextra);