From bdccc05440043d61785656d46857b27519c3559c Mon Sep 17 00:00:00 2001 From: Quinn Date: Mon, 11 Aug 2025 15:49:29 +0200 Subject: [PATCH] rework some (minor) things --- src/atrb.h | 59 +++++++++++++++++++++++++++++++++++++++++++++++ src/error.c | 18 --------------- src/error.h | 24 +++++++++++++++---- src/opts.c | 9 ++++---- src/util/intdef.h | 20 ++++++++++++++++ src/util/macro.h | 9 ++++++++ 6 files changed, 111 insertions(+), 28 deletions(-) create mode 100644 src/atrb.h delete mode 100644 src/error.c create mode 100644 src/util/intdef.h create mode 100644 src/util/macro.h diff --git a/src/atrb.h b/src/atrb.h new file mode 100644 index 0000000..9b672a4 --- /dev/null +++ b/src/atrb.h @@ -0,0 +1,59 @@ +// Copyright (c) 2025 Quinn +// Licensed under the MIT Licence. See LICENSE for details +#pragma once + +#if defined(__GNUC__) +#if __has_attribute(__pure__) +#define PURE __attribute__((__pure__)) +#else +#define PURE +#endif + +#if __has_attribute(__const__) +#define CONST __attribute__((__const__)) +#else +#define CONST +#endif + +#if __has_attribute(__noreturn__) +#define NORET __attribute__((__noreturn__)) +#else +#define NORET +#endif + +#if __has_attribute(__malloc__) +#define MALLOC __attribute__((__malloc__)) +#else +#define MALLOC +#endif + +#if __has_attribute(__used__) +#define USED __attribute__((__used__)) +#else +#define USED +#endif + +#if __has_attribute(__unused__) +#define UNUSED __attribute__((__unused__)) +#else +#define UNUSED +#endif + +#if __has_attribute(__deprecated__) +#define DEPRECATED __attribute__((__deprecated__)) +#else +#define DEPRECATED +#endif + +#if __has_attribute(__format__) +#define FORMAT(...) __attribute__((format(__VA_ARGS__))) +#else +#define FORMAT(...) +#endif + +#if __has_attribute(__nonnull__) +#define NONNULL(...) __attribute__((nonnull(__VA_ARGS__))) +#else +#define NONNULL(...) +#endif +#endif diff --git a/src/error.c b/src/error.c deleted file mode 100644 index 3ad500c..0000000 --- a/src/error.c +++ /dev/null @@ -1,18 +0,0 @@ -#include "error.h" - -#include -#include -#include -#include -#include - -noreturn void fatal(const char *fmt, ...) { - char buf[128]; - va_list args; - va_start(args, fmt); - vsnprintf(buf, 128, fmt, args); - va_end(args); - - fprintf(stderr, "\033[91mE: %s\033[0m\n", buf); - exit(1); -} diff --git a/src/error.h b/src/error.h index 07b98e8..de9a413 100644 --- a/src/error.h +++ b/src/error.h @@ -1,7 +1,21 @@ +// Copyright (c) 2025 Quinn +// Licensed under the MIT Licence. See LICENSE for details #pragma once -#include -#include -#include -// prints an error message and aborts execution -noreturn void fatal(const char *, ...); +#if __INCLUDE_LEVEL__ > 0 +#include +#include + +#include "util/macro.h" +#endif + +#define debug(s, ...) printf("\033[95m" __FILE__ ":" MACRO_STR2(__LINE__) ": [DBG]: " s "\033[0m\n", ##__VA_ARGS__) +#define info(s, ...) printf(__FILE__ ":" MACRO_STR2(__LINE__) ": [INF]: " s "\n", ##__VA_ARGS__) +#define warn(s, ...) fprintf(stderr, "\033[93m" __FILE__ ":" MACRO_STR2(__LINE__) ": [WAR]: " s "\033[0m\n", ##__VA_ARGS__) +#define error(s, ...) fprintf(stderr, "\033[91m" __FILE__ ":" MACRO_STR2(__LINE__) ": [ERR]: " s "\033[0m\n", ##__VA_ARGS__) + +#define fatal(s, ...) \ + do { \ + printf("\033[101m" __FILE__ ":" MACRO_STR2(__LINE__) ": [FAT]: " s "\033[0m\n", ##__VA_ARGS__); \ + exit(EXIT_FAILURE); \ + } while (0) diff --git a/src/opts.c b/src/opts.c index b42e2d6..1d44c69 100644 --- a/src/opts.c +++ b/src/opts.c @@ -10,23 +10,22 @@ uint8_t getoptions(int32_t argc, char *const *argv, int32_t *ncpus) { uint8_t opts = 0; char opt; - const char *const msg = "-%c has already been set"; while ((opt = getopt(argc, argv, "lavi")) != -1) { switch (opt) { case 'l': - if (opts & OPT_LIST_CORES) fatal(msg, 'l'); + if (opts & OPT_LIST_CORES) fatal("-%c has already been set", 'l'); opts |= OPT_LIST_CORES; break; case 'a': - if (opts & OPT_SET_ALL) fatal(msg, 'a'); + if (opts & OPT_SET_ALL) fatal("-%c has already been set", 'a'); opts |= OPT_SET_ALL; break; case 'v': - if (opts & OPT_VERBOSE) fatal(msg, 'v'); + if (opts & OPT_VERBOSE) fatal("-%c has already been set", 'v'); opts |= OPT_VERBOSE | OPT_LIST_CORES; break; case 'i': - if (opts & OPT_INVERT) fatal(msg, 'i'); + if (opts & OPT_INVERT) fatal("-%c has already been set", 'i'); opts |= OPT_INVERT; break; case '?': diff --git a/src/util/intdef.h b/src/util/intdef.h new file mode 100644 index 0000000..d4d4391 --- /dev/null +++ b/src/util/intdef.h @@ -0,0 +1,20 @@ +// Copyright (c) 2025 Quinn +// Licensed under the MIT Licence. See LICENSE for details +#pragma once + +/* variable-width integer types */ +typedef unsigned int uint; // ≥16 bit unsigned integer +typedef unsigned long ulong; // ≥32 bit unsigned integer +typedef signed long long llong; // ≥64 bit signed integer +typedef unsigned long long ullong; // ≥64 bit unsigned integer + +/* fixed-width integer types */ +#include +typedef int8_t i8; +typedef int16_t i16; +typedef int32_t i32; +typedef int64_t i64; +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; diff --git a/src/util/macro.h b/src/util/macro.h new file mode 100644 index 0000000..cdb271b --- /dev/null +++ b/src/util/macro.h @@ -0,0 +1,9 @@ +// Copyright (c) 2025 Quinn +// Licensed under the MIT Licence. See LICENSE for details +#pragma once + +#define WIDTHOF(t) (sizeof(t) * 8) // gets the bit width of a type +#define MACRO_CAT(x, y) x##y // concatenate two macro variables together +#define MACRO_CAT2(x, y) MACRO_CAT(x, y) // concatenate two macro variables together +#define MACRO_STR(v) #v // for converting macro variable into a string +#define MACRO_STR2(v) MACRO_STR(v) // for a recursive string generation