Initial commit and conversion
This commit is contained in:
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) [year] [fullname]
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
76
Makefile
Normal file
76
Makefile
Normal file
@@ -0,0 +1,76 @@
|
||||
# Compiler Flags
|
||||
CC := gcc
|
||||
CFLAGS := -g -Wall -Wextra -Werror -pedantic -fsanitize=address,undefined -fno-omit-frame-pointer
|
||||
|
||||
# Directory variables
|
||||
LIBDIR := lib
|
||||
OBJ := obj
|
||||
INC := include
|
||||
SRC := src
|
||||
TEST := tests
|
||||
|
||||
# Filepath Pattern Matching
|
||||
LIB := $(LIBDIR)/lib.a
|
||||
SRCS := $(wildcard $(SRC)/*.c)
|
||||
OBJS := $(patsubst $(SRC)/%.c, $(OBJ)/%.o, $(SRCS))
|
||||
TESTS := $(wildcard $(TEST)/*.c)
|
||||
TESTBINS := $(patsubst $(TEST)/%.c, $(TEST)/bin/%, $(TESTS))
|
||||
|
||||
# Commands must be labeled PHONY
|
||||
.PHONY: all release clean test
|
||||
|
||||
# Compiler Release Flags
|
||||
release: CFLAGS := -Wall -Wextra -Werror -pedantic -fsanitize=address,undefined -fno-omit-frame-pointer -O2 -DNDEBUG
|
||||
release: clean $(LIB)
|
||||
|
||||
# Target for compilation.
|
||||
all: $(LIB)
|
||||
|
||||
# Target / Dependencies
|
||||
$(LIB): $(OBJS) | $(LIBDIR)
|
||||
$(RM) $(LIB)
|
||||
ar -cvrs $@ $^
|
||||
|
||||
$(OBJ)/%.o: $(SRC)/%.c $(SRC)/%.h | $(OBJ)
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
$(OBJ)/%.o: $(SRC)/%.c | $(OBJ)
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
$(TEST)/bin/%: $(TEST)/%.c $(LIB) | $(TEST)/bin
|
||||
$(CC) $(CFLAGS) $< $(LIB) -o $@
|
||||
|
||||
# Make directories if none.
|
||||
$(LIBDIR):
|
||||
mkdir $@
|
||||
|
||||
$(INC):
|
||||
mkdir $@
|
||||
|
||||
$(OBJ):
|
||||
mkdir $@
|
||||
|
||||
$(TEST)/bin:
|
||||
mkdir $@
|
||||
|
||||
# Run the tests in the bin folder and track results
|
||||
test: $(LIB) $(TEST)/bin $(TESTBINS)
|
||||
@SUCCESS_COUNT=0; FAILURE_COUNT=0; \
|
||||
for test in $(TESTBINS); do \
|
||||
./$$test; \
|
||||
EXIT_CODE=$$?; \
|
||||
TEST_NAME=$(notdir $$test); \
|
||||
if [ $$EXIT_CODE -eq 0 ]; then \
|
||||
echo "\033[0;32m$$TEST_NAME: EXIT CODE: $$EXIT_CODE (SUCCESS)\033[0m"; \
|
||||
SUCCESS_COUNT=$$((SUCCESS_COUNT + 1)); \
|
||||
else \
|
||||
echo "\033[0;31m$$TEST_NAME: EXIT CODE: $$EXIT_CODE (FAILURE)\033[0m"; \
|
||||
FAILURE_COUNT=$$((FAILURE_COUNT + 1)); \
|
||||
fi; \
|
||||
done; \
|
||||
echo "\n\nTests completed"; \
|
||||
echo "SUCCESS: $$SUCCESS_COUNT"; \
|
||||
echo "FAILURE: $$FAILURE_COUNT";
|
||||
|
||||
clean:
|
||||
$(RM) -r $(LIBDIR) $(OBJ) $(TEST)/bin/
|
||||
25
README.md
Normal file
25
README.md
Normal file
@@ -0,0 +1,25 @@
|
||||
# except
|
||||
|
||||
## Description
|
||||
Exceptions and Try/Catch implementation written in C.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Description](#description)
|
||||
- [Features](#features)
|
||||
- [Usage](#usage)
|
||||
- [Credits / Resources](#credits--resources)
|
||||
- [License](#license)
|
||||
|
||||
## Features
|
||||
|
||||
## Usage
|
||||
|
||||
## Credits / Resources
|
||||
[Tom Preston-Werner README Driven Development](https://tom.preston-werner.com/2010/08/23/readme-driven-development)<br>
|
||||
[Make a README](https://www.makeareadme.com/)<br>
|
||||
[Choose a LICENSE](https://choosealicense.com/)<br>
|
||||
[Interfaces and Implementations - David Hanson](https://github.com/drh/cii)<br>
|
||||
|
||||
## License
|
||||
This project is licensed under MIT - see the [LICENSE](LICENSE) file for details.
|
||||
103
include/except.h
Normal file
103
include/except.h
Normal file
@@ -0,0 +1,103 @@
|
||||
#ifndef EXCEPT_INCLUDED
|
||||
#define EXCEPT_INCLUDED
|
||||
|
||||
#include <setjmp.h>
|
||||
struct Exception {
|
||||
const char *reason;
|
||||
};
|
||||
typedef struct Exception Exception;
|
||||
|
||||
struct Except_Frame {
|
||||
struct Except_Frame *prev;
|
||||
jmp_buf env;
|
||||
const char *file;
|
||||
int line;
|
||||
const struct Exception *exception;
|
||||
};
|
||||
typedef struct Except_Frame Except_Frame;
|
||||
|
||||
enum {
|
||||
EXCEPT_STATE_ENTERED=0,
|
||||
EXCEPT_STATE_RAISED,
|
||||
EXCEPT_STATE_HANDLED,
|
||||
EXCEPT_STATE_FINALIZED,
|
||||
EXCEPT_STATE_COUNT };
|
||||
|
||||
extern struct Except_Frame *except_stack;
|
||||
extern const struct Exception assert_failed;
|
||||
void except_raise(const struct Exception *e, const char *file, int line);
|
||||
|
||||
// Try Catch MACROS - LINUX
|
||||
#define RAISE(e) except_raise(&(e), __FILE__, __LINE__)
|
||||
#define RERAISE except_raise(Except_frame.exception, \
|
||||
Except_frame.file, Except_frame.line)
|
||||
#define RETURN switch (Except_stack = Except_stack->prev,0) default: return
|
||||
#define TRY do { \
|
||||
volatile int Except_flag; \
|
||||
Except_Frame Except_frame; \
|
||||
Except_frame.prev = Except_stack; \
|
||||
Except_stack = &Except_frame; \
|
||||
Except_flag = setjmp(Except_frame.env); \
|
||||
if (Except_flag == EXCEPT_STATE_ENTERED) {
|
||||
#define EXCEPT(e) \
|
||||
if (Except_flag == EXCEPT_STATE_ENTERED) Except_stack = Except_stack->prev; \
|
||||
} else if (Except_frame.exception == &(e)) { \
|
||||
Except_flag = EXCEPT_STATE_HANDLED;
|
||||
#define ELSE \
|
||||
if (Except_flag == EXCEPT_STATE_ENTERED) Except_stack = Except_stack->prev; \
|
||||
} else { \
|
||||
Except_flag = EXCEPT_STATE_HANDLED;
|
||||
#define FINALLY \
|
||||
if (Except_flag == EXCEPT_STATE_ENTERED) Except_stack = Except_stack->prev; \
|
||||
} { \
|
||||
if (Except_flag == EXCEPT_STATE_ENTERED) \
|
||||
Except_flag = EXCEPT_STATE_FINALIZED;
|
||||
#define END_TRY \
|
||||
if (Except_flag == EXCEPT_STATE_ENTERED) Except_stack = Except_stack->prev; \
|
||||
} if (Except_flag == EXCEPT_STATE_RAISED) RERAISE; \
|
||||
} while (0)
|
||||
|
||||
|
||||
// Try Catch MACROS - WIN32
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
|
||||
extern int except_index;
|
||||
extern void except_init(void);
|
||||
extern void except_push(Except_Frame *fp);
|
||||
extern void except_pop(void);
|
||||
|
||||
#define RAISE(e) except_raise(&(e), __FILE__, __LINE__)
|
||||
#define RERAISE except_raise(Except_frame.exception, \
|
||||
Except_frame.file, Except_frame.line)
|
||||
#define RETURN switch (Except_pop(),0) default: return
|
||||
#define TRY do { \
|
||||
volatile int Except_flag; \
|
||||
Except_Frame Except_frame; \
|
||||
if (Except_index == -1) \
|
||||
Except_init(); \
|
||||
Except_push(&Except_frame); \
|
||||
Except_flag = setjmp(Except_frame.env); \
|
||||
if (Except_flag == EXCEPT_STATE_ENTERED) {
|
||||
#define EXCEPT(e) \
|
||||
if (Except_flag == EXCEPT_STATE_ENTERED) Except_pop(); \
|
||||
} else if (Except_frame.exception == &(e)) { \
|
||||
Except_flag = EXCEPT_STATE_HANDLED;
|
||||
#define ELSE \
|
||||
if (Except_flag == EXCEPT_STATE_ENTERED) Except_pop(); \
|
||||
} else { \
|
||||
Except_flag = EXCEPT_STATE_HANDLED;
|
||||
#define FINALLY \
|
||||
if (Except_flag == EXCEPT_STATE_ENTERED) Except_pop(); \
|
||||
} { \
|
||||
if (Except_flag == EXCEPT_STATE_ENTERED) \
|
||||
Except_flag = EXCEPT_STATE_FINALIZED;
|
||||
#define END_TRY \
|
||||
if (Except_flag == EXCEPT_STATE_ENTERED) Except_pop(); \
|
||||
} if (Except_flag == EXCEPT_STATE_RAISED) RERAISE; \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif // except.h
|
||||
71
src/except.c
Normal file
71
src/except.c
Normal file
@@ -0,0 +1,71 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h> // for now
|
||||
#include "../include/except.h"
|
||||
|
||||
struct Except_Frame *except_stack = NULL;
|
||||
void except_raise(const struct Exception *e, const char *file,int line)
|
||||
{
|
||||
#ifdef WIN32
|
||||
struct Except_Frame *p;
|
||||
|
||||
if (except_index == -1)
|
||||
except_init();
|
||||
p = TlsGetValue(except_index);
|
||||
#else
|
||||
struct Except_Frame *p = except_stack;
|
||||
#endif
|
||||
assert(e);
|
||||
if (p == NULL) {
|
||||
fprintf(stderr, "Uncaught exception");
|
||||
if (e->reason)
|
||||
fprintf(stderr, " %s", e->reason);
|
||||
else
|
||||
fprintf(stderr, " at 0x%p", (void*)e);
|
||||
if (file && line > 0)
|
||||
fprintf(stderr, " raised at %s:%d\n", file, line);
|
||||
fprintf(stderr, "aborting...\n");
|
||||
fflush(stderr);
|
||||
abort();
|
||||
}
|
||||
p->exception = e;
|
||||
p->file = file;
|
||||
p->line = line;
|
||||
#ifdef WIN32
|
||||
except_pop();
|
||||
#else
|
||||
except_stack = except_stack->prev;
|
||||
#endif
|
||||
longjmp(p->env, EXCEPT_STATE_RAISED);
|
||||
}
|
||||
#ifdef WIN32
|
||||
_CRTIMP void __cdecl _assert(void *, void *, unsigned);
|
||||
#undef assert
|
||||
#define assert(e) ((e) || (_assert(#e, __FILE__, __LINE__), 0))
|
||||
|
||||
int except_index = -1;
|
||||
void except_init(void) {
|
||||
BOOL cond;
|
||||
|
||||
except_index = TlsAlloc();
|
||||
assert(except_index != TLS_OUT_OF_INDEXES);
|
||||
cond = TlsSetValue(except_index, NULL);
|
||||
assert(cond == TRUE);
|
||||
}
|
||||
|
||||
void Except_push(Except_Frame *fp) {
|
||||
BOOL cond;
|
||||
|
||||
fp->prev = TlsGetValue(except_index);
|
||||
cond = TlsSetValue(except_index, fp);
|
||||
assert(cond == TRUE);
|
||||
}
|
||||
|
||||
void Except_pop(void) {
|
||||
BOOL cond;
|
||||
Except_Frame *tos = TlsGetValue(Except_index);
|
||||
|
||||
cond = TlsSetValue(except_index, tos->prev);
|
||||
assert(cond == TRUE);
|
||||
}
|
||||
#endif
|
||||
14
tests/01_sanity.c
Normal file
14
tests/01_sanity.c
Normal file
@@ -0,0 +1,14 @@
|
||||
#define DEBUG
|
||||
#include "../include/except.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
int main(void){
|
||||
printf("\n\n");
|
||||
printf("Assertion sanity test.\n");
|
||||
assert(NULL);
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
}
|
||||
14
tests/02_uncaught.c
Normal file
14
tests/02_uncaught.c
Normal file
@@ -0,0 +1,14 @@
|
||||
#define DEBUG
|
||||
#include "../include/except.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void){
|
||||
printf("\n\n");
|
||||
printf("Uncaught exceptions sanity test.\n");
|
||||
Exception u = {NULL};
|
||||
RAISE(u);
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user