assert.h progress

This commit is contained in:
bumbread 2022-06-02 16:18:26 +11:00
parent fc03be076a
commit f8aef19b5c
25 changed files with 176 additions and 0 deletions

46
code/assert.c Normal file
View File

@ -0,0 +1,46 @@
#include <assert.h>
#include <stdlib.h>
#include <_platform.h>
#if defined(_os_win)
#define WIN32_LEAN_AND_MEAN
#include<Windows.h>
// TODO: make it work via printf
static int _assert_strlen(char const *str)
{
int len = 0;
while(*str++!=0) ++len;
return len;
}
static void _assert_win32_prints(HANDLE conout, char const *str)
{
DWORD written = 0;
WriteConsole(conout, str, _assert_strlen(str), &written, NULL);
}
extern void _assert_print(char *cond, char const *func, char const *file, char const *line)
{
HANDLE conout = GetStdHandle(STD_OUTPUT_HANDLE);
if(conout != INVALID_HANDLE_VALUE) {
_assert_win32_prints(conout, "Assertion Failed: ");
_assert_win32_prints(conout, cond);
_assert_win32_prints(conout, "\n\tFile: ");
_assert_win32_prints(conout, file);
_assert_win32_prints(conout, "\n\tFunc: ");
_assert_win32_prints(conout, func);
_assert_win32_prints(conout, "\n\tLine: ");
_assert_win32_prints(conout, line);
}
else {
// TODO:
MessageBoxA(NULL,
"Assertion Failed Somewhere, good luck finding it!\n",
"Error",
MB_OK);
}
}
#endif

9
code/entry_windows.c Normal file
View File

@ -0,0 +1,9 @@
#include<_platform.h>
extern int main();
int mainCRTStartup()
{
return main();
}

47
inc/_platform.h Normal file
View File

@ -0,0 +1,47 @@
#if !defined(_platform_h)
#define _platform_h
// Compiler Identification
#if defined(_MSC_VER) && !defined(__clang__)
#define _compiler_msvc
#endif
#if defined(__GNUC__) && !defined(__clang__)
#define _compiler_gnu
#endif
#if defined(__clang__)
#define _compiler_clang
#endif
#if !(defined(_compiler_msvc) \
|| defined(_compiler_gnu) \
|| defined(_compiler_clang))
#error "Unsupported Compiler"
#endif
#if !defined(_func)
#if defined(_compiler_msvc)
#define _func __FUNCTION__
#else
#define _func __func__
#endif
#endif
// OS Identification
#if defined(_WIN32)
#define _os_win
#endif
#if defined(__linux__) && !defined(__ANDROID__)
#define _os_linux
#endif
#if !(defined(_os_win) \
|| defined(_os_linux))
#error "Unsupported OS"
#endif
#endif

32
inc/assert.h Normal file
View File

@ -0,0 +1,32 @@
#if !defined(_assert_h)
#define _assert_h
#include "_platform.h"
extern void _assert_print(char *cond, char const *func, char const *file, char const *line);
// TODO: use abort() to break
#if defined(_compiler_msvc)
#include <intrin.h>
#define _compiler_brk __debugbreak
#else
#define _compiler_brk __builtin_trap
#endif
#define _str_(x) #x
#define _str(x) _str_(x)
#if defined(NDEBUG)
#define assert(ignore) ((void)0)
#else
#define _static_assert _Static_assert
#define assert(condition) \
do { \
if(!(condition)) { \
_assert_print(#condition, _func, __FILE__, _str(__LINE__)); \
_compiler_brk(); \
} \
} while(0)
#endif
#endif

2
inc/complex.h Normal file
View File

@ -0,0 +1,2 @@
#error "Complex Numbers aren't implemented"

0
inc/ctype.h Normal file
View File

0
inc/errno.h Normal file
View File

0
inc/fenv.h Normal file
View File

0
inc/inttypes.h Normal file
View File

0
inc/locale.h Normal file
View File

0
inc/math.h Normal file
View File

0
inc/signal.h Normal file
View File

0
inc/stdatomic.h Normal file
View File

4
inc/stdio.h Normal file
View File

@ -0,0 +1,4 @@
#define EOF -1

2
inc/stdlib.h Normal file
View File

@ -0,0 +1,2 @@
#define NULL ((void *)0)

0
inc/string.h Normal file
View File

0
inc/tgmath.h Normal file
View File

0
inc/threads.h Normal file
View File

0
inc/time.h Normal file
View File

0
inc/uchar.h Normal file
View File

0
inc/wchar.h Normal file
View File

0
inc/wctype.h Normal file
View File

20
makefile Normal file
View File

@ -0,0 +1,20 @@
GNUFLAGS=-Iinc -Icode
CLFLAGS=/link /incremental:no /subsystem:windows /nodefaultlib kernel32.lib
CC=clang
CFLAGS=$(GNUFLAGS)
LDFLAGS=/nologo /nodefaultlib /entry:mainCRTStartup
SRC_DIR := code
OBJ_DIR := build
SRC_FILES := $(wildcard $(SRC_DIR)/*.c)
OBJ_FILES := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRC_FILES))
ciabatta.lib: $(OBJ_FILES)
lib $(LDFLAGS) /out:lib/$@ $^
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
$(CC) $(CFLAGS) -c -o $@ $<
.PHONY: ciabatta.lib

6
test/bake.bat Normal file
View File

@ -0,0 +1,6 @@
@echo off
cl /c /I ..\inc /nologo /Gm- /GR- /EHa- /Oi /W4 /Z7 test.c
clang -c -Wall test.c -otest1.obj
link /nologo test.obj /nodefaultlib user32.lib kernel32.lib ..\lib\ciabatta.lib -subsystem:console
link /nologo test1.obj /nodefaultlib user32.lib kernel32.lib ..\lib\ciabatta.lib -subsystem:console

8
test/test.c Normal file
View File

@ -0,0 +1,8 @@
// MSVC won't use my <assert.h> unless i provide path explicitly
// idk
#include"..\inc\assert.h"
int main()
{
assert(0);
return 0;
}