style: add clang-format
This commit is contained in:
parent
3a74610390
commit
18fdefe286
|
@ -0,0 +1,68 @@
|
|||
# alignment
|
||||
AlignAfterOpenBracket: Align
|
||||
AlignArrayOfStructures: Left
|
||||
AlignConsecutiveAssignments: None
|
||||
AlignConsecutiveBitFields: Consecutive
|
||||
AlignConsecutiveDeclarations: None
|
||||
AlignConsecutiveMacros: Consecutive
|
||||
AlignEscapedNewlines: Right
|
||||
AlignOperands: Align
|
||||
AlignTrailingComments: true
|
||||
# wrapping and line breaks
|
||||
AllowAllArgumentsOnNextLine: true
|
||||
AllowAllParametersOfDeclarationOnNextLine: false
|
||||
AlwaysBreakAfterReturnType: AllDefinitions
|
||||
AlwaysBreakBeforeMultilineStrings: false
|
||||
BinPackArguments: true
|
||||
BinPackParameters: false
|
||||
BreakBeforeBinaryOperators: NonAssignment
|
||||
BreakBeforeBraces: WebKit
|
||||
BreakBeforeTernaryOperators: true
|
||||
BreakStringLiterals: true
|
||||
ColumnLimit: 100
|
||||
IndentWrappedFunctionNames: false
|
||||
ReflowComments: true
|
||||
# braces and blocks
|
||||
AllowShortBlocksOnASingleLine: Empty
|
||||
AllowShortCaseLabelsOnASingleLine: false
|
||||
AllowShortEnumsOnASingleLine: false
|
||||
AllowShortFunctionsOnASingleLine: Empty
|
||||
AllowShortIfStatementsOnASingleLine: Never
|
||||
InsertBraces: true
|
||||
# spacing
|
||||
BitFieldColonSpacing: Both
|
||||
Cpp11BracedListStyle: false
|
||||
DerivePointerAlignment: false
|
||||
IncludeBlocks: Merge
|
||||
#InsertTrailingCommas: Wrapped
|
||||
KeepEmptyLinesAtTheStartOfBlocks: false
|
||||
MaxEmptyLinesToKeep: 1
|
||||
PointerAlignment: Right
|
||||
QualifierAlignment: Left
|
||||
SeparateDefinitionBlocks: Always
|
||||
SortIncludes: CaseSensitive
|
||||
SpaceAfterCStyleCast: true
|
||||
SpaceAfterLogicalNot: true
|
||||
SpaceAroundPointerQualifiers: Default
|
||||
SpaceBeforeAssignmentOperators: true
|
||||
SpaceBeforeCaseColon: false
|
||||
SpaceBeforeParens: Always
|
||||
SpaceBeforeSquareBrackets: false
|
||||
SpaceInEmptyBlock: true
|
||||
SpaceInEmptyParentheses: false
|
||||
SpacesBeforeTrailingComments: 1
|
||||
SpacesInCStyleCastParentheses: false
|
||||
SpacesInConditionalStatement: false
|
||||
SpacesInLineCommentPrefix: { Minimum: 1, Maximum: 1 }
|
||||
SpacesInParentheses: false
|
||||
SpacesInSquareBrackets: false
|
||||
# indentation
|
||||
ContinuationIndentWidth: 2
|
||||
IndentCaseBlocks: false
|
||||
IndentCaseLabels: true
|
||||
IndentGotoLabels: false
|
||||
IndentPPDirectives: AfterHash
|
||||
IndentWidth: 2
|
||||
PPIndentWidth: 2
|
||||
TabWidth: 2
|
||||
UseTab: Never
|
52
inc/common.h
52
inc/common.h
|
@ -1,60 +1,56 @@
|
|||
#ifndef COMMON_H_
|
||||
#define COMMON_H_
|
||||
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
|
||||
/** macros ********************************************************************/
|
||||
/** macros ****************************************************************************************/
|
||||
|
||||
/// define debug macros
|
||||
|
||||
#ifdef DEBUG
|
||||
# define DEBUG_ALLOC
|
||||
# define DEBUG_ALLOC
|
||||
#endif
|
||||
|
||||
/// c11 / c23 compatibility. many of these can be removed once the project fully
|
||||
/// migrates to c23, but for now, some of these will have to be used. when
|
||||
/// migrating, remember that the usage of macros like `_Noreturn` should be
|
||||
/// replaced by c23-specific syntax, in this case the `[[noreturn]]` attribute.
|
||||
/// c11 / c23 compatibility. many of these can be removed once the project fully migrates to c23,
|
||||
/// but for now, some of these will have to be used. when migrating, remember that the usage of
|
||||
/// macros like `_Noreturn` should be replaced by c23-specific syntax, in this case the
|
||||
/// `[[noreturn]]` attribute.
|
||||
|
||||
// if the program is compiled with c23 or newer, define `STDC23` as a feature
|
||||
// test macro that is used for enabling features based on if c23 or c11 is used.
|
||||
// if the program is compiled with c23 or newer, define `STDC23` as a feature test macro that is
|
||||
// used for enabling features based on if c23 or c11 is used.
|
||||
|
||||
#if __STDC_VERSION__ >= 202000L
|
||||
# define STDC23
|
||||
# define STDC23
|
||||
#endif
|
||||
|
||||
// enable C11-specific features
|
||||
|
||||
#if !defined(STDC23)
|
||||
# define nullptr NULL /* `nullptr` from C23 */
|
||||
#if ! defined(STDC23)
|
||||
# define nullptr NULL /* `nullptr` from C23 */
|
||||
#endif
|
||||
|
||||
// enable C23-specific features
|
||||
|
||||
#if defined(STDC23)
|
||||
# define _Noreturn [[noreturn]]
|
||||
# define _Noreturn [[noreturn]]
|
||||
#endif
|
||||
|
||||
|
||||
/// function wrappers
|
||||
|
||||
#ifdef DEBUG_ALLOC
|
||||
# define smalloc(nmemb, size) __smalloc (nmemb, size, __FILE__, __LINE__)
|
||||
# define scalloc(nmemb, size) __scalloc (nmemb, size, __FILE__, __LINE__)
|
||||
# define srealloc(ptr, nmemb, size) __srealloc (ptr, nmemb, size, __FILE__, __LINE__)
|
||||
# define smalloc(nmemb, size) __smalloc (nmemb, size, __FILE__, __LINE__)
|
||||
# define scalloc(nmemb, size) __scalloc (nmemb, size, __FILE__, __LINE__)
|
||||
# define srealloc(ptr, nmemb, size) __srealloc (ptr, nmemb, size, __FILE__, __LINE__)
|
||||
#else
|
||||
# define smalloc(nmemb, size) __smalloc (nmemb, size)
|
||||
# define scalloc(nmemb, size) __scalloc (nmemb, size)
|
||||
# define srealloc(ptr, nmemb, size) __srealloc (ptr, nmemb, size)
|
||||
# define smalloc(nmemb, size) __smalloc (nmemb, size)
|
||||
# define scalloc(nmemb, size) __scalloc (nmemb, size)
|
||||
# define srealloc(ptr, nmemb, size) __srealloc (ptr, nmemb, size)
|
||||
#endif
|
||||
|
||||
|
||||
/** typedefs ******************************************************************/
|
||||
/** typedefs **************************************************************************************/
|
||||
|
||||
/// common type abbreviations
|
||||
|
||||
|
@ -88,15 +84,14 @@ typedef float f32;
|
|||
typedef double f64;
|
||||
typedef long double f128;
|
||||
|
||||
|
||||
/** functions *****************************************************************/
|
||||
/** functions *************************************************************************************/
|
||||
|
||||
/// safe memory allocation
|
||||
|
||||
#ifdef DEBUG_ALLOC
|
||||
void *__smalloc (const usize nmemb, const usize size, const char *const file, const usize line);
|
||||
void *__scalloc (const usize nmemb, const usize size, const char *const file, const usize line);
|
||||
void *__srealloc (void *ptr, const usize nmemb, const usize size, const char *const file, const usize line);
|
||||
void *__smalloc (const usize nmemb, const usize size, char *file, usize line);
|
||||
void *__scalloc (const usize nmemb, const usize size, char *file, usize line);
|
||||
void *__srealloc (void *ptr, const usize nmemb, const usize size, char *file, usize line);
|
||||
void sfree (void *ptr);
|
||||
#else
|
||||
void *__smalloc (const usize nmemb, const usize size);
|
||||
|
@ -105,5 +100,4 @@ void *__srealloc (void *ptr, const usize nmemb, const usize size);
|
|||
void sfree (void *ptr);
|
||||
#endif
|
||||
|
||||
|
||||
#endif // COMMON_H_
|
||||
|
|
38
inc/log.h
38
inc/log.h
|
@ -1,39 +1,35 @@
|
|||
#ifndef LOG_H_
|
||||
#define LOG_H_
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
/** macros ********************************************************************/
|
||||
/** macros ****************************************************************************************/
|
||||
|
||||
#define log_debug(...) __log_print (__Log_Level_Debug, __VA_ARGS__)
|
||||
#define log_info(...) __log_print (__Log_Level_Info, __VA_ARGS__)
|
||||
#define log_ok(...) __log_print (__Log_Level_Ok, __VA_ARGS__)
|
||||
#define log_warn(...) __log_print (__Log_Level_Warn, __VA_ARGS__)
|
||||
#define log_err(...) __log_print (__Log_Level_Err, __VA_ARGS__)
|
||||
#define log_info(...) __log_print (__Log_Level_Info, __VA_ARGS__)
|
||||
#define log_ok(...) __log_print (__Log_Level_Ok, __VA_ARGS__)
|
||||
#define log_warn(...) __log_print (__Log_Level_Warn, __VA_ARGS__)
|
||||
#define log_err(...) __log_print (__Log_Level_Err, __VA_ARGS__)
|
||||
|
||||
#define log_die(...) \
|
||||
do { \
|
||||
__log_print(__Log_Level_Err, __VA_ARGS__); \
|
||||
exit(EXIT_FAILURE); \
|
||||
#define log_die(...) \
|
||||
do { \
|
||||
__log_print (__Log_Level_Err, __VA_ARGS__); \
|
||||
exit (EXIT_FAILURE); \
|
||||
} while (0)
|
||||
|
||||
|
||||
/** enums *********************************************************************/
|
||||
/** enums *****************************************************************************************/
|
||||
|
||||
enum __Log_Level {
|
||||
__Log_Level_Debug,
|
||||
__Log_Level_Info,
|
||||
__Log_Level_Ok,
|
||||
__Log_Level_Warn,
|
||||
__Log_Level_Err,
|
||||
__Log_Level_Debug,
|
||||
__Log_Level_Info,
|
||||
__Log_Level_Ok,
|
||||
__Log_Level_Warn,
|
||||
__Log_Level_Err,
|
||||
};
|
||||
|
||||
|
||||
/** functions *****************************************************************/
|
||||
|
||||
void __log_print (enum __Log_Level level, const char *const fmt, ...) __attribute__ ((format (printf, 2, 3)));
|
||||
|
||||
void __log_print (enum __Log_Level level, const char *const fmt, ...)
|
||||
__attribute__ ((format (printf, 2, 3)));
|
||||
|
||||
#endif // LOG_H_
|
||||
|
|
57
src/common.c
57
src/common.c
|
@ -1,24 +1,24 @@
|
|||
#include "common.h"
|
||||
#include "log.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include "log.h"
|
||||
|
||||
|
||||
/** functions *****************************************************************/
|
||||
/** functions *************************************************************************************/
|
||||
|
||||
/**
|
||||
* safe `malloc()` wrapper. instead of returning `nullptr` on error, this
|
||||
* function will crash the program. returns a pointer to the allocated memory.
|
||||
* safe `malloc()` wrapper. instead of returning `nullptr` on error, this function will crash the
|
||||
* program. returns a pointer to the allocated memory.
|
||||
*/
|
||||
void *
|
||||
__smalloc (const usize nmemb, /* number of elements to allocate */
|
||||
const usize size /* size of each element */
|
||||
__smalloc (const usize nmemb, /* number of elements to allocate */
|
||||
const usize size /* size of each element */
|
||||
#ifdef DEBUG_ALLOC
|
||||
,
|
||||
const char *const file, /* __FILE__ */
|
||||
const usize line /* __LINE__ */
|
||||
char *file, /* __FILE__ */
|
||||
usize line /* __LINE__ */
|
||||
#endif
|
||||
) {
|
||||
)
|
||||
{
|
||||
void *ptr;
|
||||
|
||||
ptr = malloc (nmemb * size);
|
||||
|
@ -36,18 +36,19 @@ __smalloc (const usize nmemb, /* number of elements to allocate */
|
|||
}
|
||||
|
||||
/**
|
||||
* safe `calloc()` wrapper. instead of returning `nullptr` on error, this
|
||||
* function will crash the program. returns a pointer to the allocated memory.
|
||||
* safe `calloc()` wrapper. instead of returning `nullptr` on error, this function will crash the
|
||||
* program. returns a pointer to the allocated memory.
|
||||
*/
|
||||
void *
|
||||
__scalloc (const usize nmemb, /* number of elements to allocate */
|
||||
const usize size /* size of each element */
|
||||
__scalloc (const usize nmemb, /* number of elements to allocate */
|
||||
const usize size /* size of each element */
|
||||
#ifdef DEBUG_ALLOC
|
||||
,
|
||||
const char *const file, /* __FILE__ */
|
||||
const usize line /* __LINE__ */
|
||||
char *file, /* __FILE__ */
|
||||
usize line /* __LINE__ */
|
||||
#endif
|
||||
) {
|
||||
)
|
||||
{
|
||||
void *ptr;
|
||||
|
||||
ptr = calloc (nmemb, size);
|
||||
|
@ -65,19 +66,20 @@ __scalloc (const usize nmemb, /* number of elements to allocate */
|
|||
}
|
||||
|
||||
/**
|
||||
* safe `realloc()` wrapper. instead of returning `nullptr` on error, this
|
||||
* function will crash the program. returns a pointer to the reallocated memory.
|
||||
* safe `realloc()` wrapper. instead of returning `nullptr` on error, this function will crash the
|
||||
* program. returns a pointer to the reallocated memory.
|
||||
*/
|
||||
void *
|
||||
__srealloc (void *ptr, /* the pointer to reallocate */
|
||||
const usize nmemb, /* number of elements to allocate */
|
||||
const usize size /* size of each element */
|
||||
__srealloc (void *ptr, /* the pointer to reallocate */
|
||||
const usize nmemb, /* number of elements to allocate */
|
||||
const usize size /* size of each element */
|
||||
#ifdef DEBUG_ALLOC
|
||||
,
|
||||
const char *const file, /* __FILE__ */
|
||||
const usize line /* __LINE__ */
|
||||
char *file, /* __FILE__ */
|
||||
usize line /* __LINE__ */
|
||||
#endif
|
||||
) {
|
||||
)
|
||||
{
|
||||
ptr = realloc (ptr, nmemb * size);
|
||||
if (ptr == nullptr) {
|
||||
#ifdef DEBUG_ALLOC
|
||||
|
@ -93,9 +95,8 @@ __srealloc (void *ptr, /* the pointer to reallocate */
|
|||
}
|
||||
|
||||
/**
|
||||
* wrapper for `free()`. will not call `free()` on pointers that are `nullptr`.
|
||||
* this is technically not necessary according to the c standard, but you never
|
||||
* know.
|
||||
* wrapper for `free()`. will not call `free()` on pointers that are `nullptr`. this is technically
|
||||
* not necessary according to the c standard, but you never know.
|
||||
*/
|
||||
void
|
||||
sfree (void *ptr) /* the pointer to free */
|
||||
|
|
23
src/log.c
23
src/log.c
|
@ -3,29 +3,28 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
/** functions *****************************************************************/
|
||||
/** functions *************************************************************************************/
|
||||
|
||||
/**
|
||||
* print a formatted message to stderr using printf formatting and a given log
|
||||
* level. this functions is not intended to be used directly. the `log_*` macros
|
||||
* should be used instead.
|
||||
* print a formatted message to stderr using printf formatting and a given log level. this functions
|
||||
* is not intended to be used directly. the `log_*` macros should be used instead.
|
||||
*/
|
||||
__attribute__ ((format (printf, 2, 3)))
|
||||
void
|
||||
__attribute__ ((format (printf, 2, 3))) void
|
||||
__log_print (enum __Log_Level level, /* log level */
|
||||
const char *const fmt, /* format string */
|
||||
...) /* format parameters */
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
// clang-format off
|
||||
switch (level) {
|
||||
case __Log_Level_Debug: { fprintf (stderr, "\x1b[90m[\x1b[35mdebug\x1b[90m]\x1b[0m "); break; }
|
||||
case __Log_Level_Info: { fprintf (stderr, "\x1b[90m[\x1b[34minfo\x1b[90m]\x1b[0m " ); break; }
|
||||
case __Log_Level_Ok: { fprintf (stderr, "\x1b[90m[\x1b[32mok\x1b[90m]\x1b[0m " ); break; }
|
||||
case __Log_Level_Warn: { fprintf (stderr, "\x1b[90m[\x1b[33mwarn\x1b[90m]\x1b[0m " ); break; }
|
||||
case __Log_Level_Err: { fprintf (stderr, "\x1b[90m[\x1b[31merr\x1b[90m]\x1b[0m " ); break; }
|
||||
case __Log_Level_Debug: { fprintf (stderr, "\x1b[90m[\x1b[35mdebug\x1b[90m]\x1b[0m "); } break;
|
||||
case __Log_Level_Info: { fprintf (stderr, "\x1b[90m[\x1b[34minfo\x1b[90m]\x1b[0m "); } break;
|
||||
case __Log_Level_Ok: { fprintf (stderr, "\x1b[90m[\x1b[32mok\x1b[90m]\x1b[0m "); } break;
|
||||
case __Log_Level_Warn: { fprintf (stderr, "\x1b[90m[\x1b[33mwarn\x1b[90m]\x1b[0m "); } break;
|
||||
case __Log_Level_Err: { fprintf (stderr, "\x1b[90m[\x1b[31merr\x1b[90m]\x1b[0m "); } break;
|
||||
}
|
||||
// clang-format on
|
||||
|
||||
va_start (ap, fmt);
|
||||
vfprintf (stderr, fmt, ap);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include <stdlib.h>
|
||||
#include "common.h"
|
||||
#include "log.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
int
|
||||
main (const int argc, const char *const argv[])
|
||||
|
|
|
@ -1,14 +1,12 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "common.h"
|
||||
#include "test.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/** declarations **************************************************************/
|
||||
|
||||
static Test_Result test_add (void);
|
||||
static u64 add(u64 a, u64 b);
|
||||
|
||||
static u64 add (u64 a, u64 b);
|
||||
|
||||
/** functions *****************************************************************/
|
||||
|
||||
|
@ -25,7 +23,6 @@ main (const int argc, const char *const argv[])
|
|||
t_exit ();
|
||||
}
|
||||
|
||||
|
||||
/** static functions **********************************************************/
|
||||
|
||||
static Test_Result
|
||||
|
|
73
test/test.h
73
test/test.h
|
@ -1,62 +1,57 @@
|
|||
#ifndef TEST_H_
|
||||
#define TEST_H_
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "common.h"
|
||||
#include "log.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
/** structs *******************************************************************/
|
||||
/** structs ***************************************************************************************/
|
||||
|
||||
typedef struct {
|
||||
usize success;
|
||||
usize failure;
|
||||
usize success;
|
||||
usize failure;
|
||||
} Test_Result;
|
||||
|
||||
/** macros ****************************************************************************************/
|
||||
|
||||
/** macros ********************************************************************/
|
||||
#define t_start() Test_Result __test_result = { .success = 0u, .failure = 0u }
|
||||
|
||||
#define t_start() \
|
||||
Test_Result __test_result = { .success = 0u, .failure = 0u }
|
||||
|
||||
#define t_assert(check, msg) \
|
||||
do { \
|
||||
if (check) { \
|
||||
__test_result.success += 1; \
|
||||
} else { \
|
||||
log_err ("\x1b[90m(\x1b[35m" __FILE__ "\x1b[90m:\x1b[34m%d\x1b[90m)" \
|
||||
"\x1b[0m assertion '" msg "' (" #check ") failed\n", __LINE__); \
|
||||
__test_result.failure += 1; \
|
||||
} \
|
||||
#define t_assert(check, msg) \
|
||||
do { \
|
||||
if (check) { \
|
||||
__test_result.success += 1; \
|
||||
} else { \
|
||||
log_err ("\x1b[90m(\x1b[35m" __FILE__ "\x1b[90m:\x1b[34m%d\x1b[90m)" \
|
||||
"\x1b[0m assertion '" msg "' (" #check ") failed\n", \
|
||||
__LINE__); \
|
||||
__test_result.failure += 1; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define t_end() \
|
||||
return __test_result
|
||||
#define t_end() return __test_result
|
||||
|
||||
#define t_run(func) \
|
||||
do { \
|
||||
Test_Result res = func (); \
|
||||
__test_results.success += res.success; \
|
||||
__test_results.failure += res.failure; \
|
||||
#define t_run(func) \
|
||||
do { \
|
||||
Test_Result res = func (); \
|
||||
__test_results.success += res.success; \
|
||||
__test_results.failure += res.failure; \
|
||||
} while (0)
|
||||
|
||||
#define t_start_tests() \
|
||||
Test_Result __test_results = { .success = 0u, .failure = 0u }
|
||||
#define t_start_tests() Test_Result __test_results = { .success = 0u, .failure = 0u }
|
||||
|
||||
#define t_end_tests() \
|
||||
do { \
|
||||
if (__test_results.failure == 0u) { \
|
||||
log_ok ("successfully executed %zu tests.\n", __test_results.success); \
|
||||
} else { \
|
||||
log_err ("%zu out of %zu tests failed.\n", __test_results.failure, \
|
||||
__test_results.success + __test_results.failure); \
|
||||
} \
|
||||
#define t_end_tests() \
|
||||
do { \
|
||||
if (__test_results.failure == 0u) { \
|
||||
log_ok ("successfully executed %zu tests.\n", __test_results.success); \
|
||||
} else { \
|
||||
log_err ("%zu out of %zu tests failed.\n", __test_results.failure, \
|
||||
__test_results.success + __test_results.failure); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define t_exit() \
|
||||
do { \
|
||||
exit (__test_results.failure == 0u ? EXIT_SUCCESS : EXIT_FAILURE); \
|
||||
#define t_exit() \
|
||||
do { \
|
||||
exit (__test_results.failure == 0u ? EXIT_SUCCESS : EXIT_FAILURE); \
|
||||
} while (0)
|
||||
|
||||
#endif // TEST_H_
|
||||
|
|
Loading…
Reference in New Issue