commit 2b3d9e6ef3b87eb105c34cd9aba0c90572c6d8dc Author: Randy Jordan Date: Fri Apr 4 08:45:31 2025 -0500 Initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8aa2645 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..24b761c --- /dev/null +++ b/Makefile @@ -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 -n "\033[0;32m$$TEST_NAME: EXIT CODE: $$EXIT_CODE (SUCCESS)\033[0m"; \ + SUCCESS_COUNT=$$((SUCCESS_COUNT + 1)); \ + else \ + echo -n "\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/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..3e030c4 --- /dev/null +++ b/README.md @@ -0,0 +1,27 @@ +# Except + +## Description +Exceptions, Try/Catch, and Testing library written in C. + +## Table of Contents + +- [Description](#description) +- [Features](#features) +- [Usage](#usage) +- [Credits / Resources](#credits--resources) +- [License](#license) + +## Features / TODOS +- [x] Exceptions with setjmp longjmp. +- [x] Try / Catch logic. +- [x] Assert based testing. + +## Usage +See /tests + +## Credits / Resources +[Tom Preston-Werner - README Driven Development](https://tom.preston-werner.com/2010/08/23/readme-driven-development.html)
+[C Interfaces and Implementations](https://github.com/drh/cii)
+ +## License +This project is licensed under MIT - see the [LICENSE](LICENSE) file for details. diff --git a/include/except.h b/include/except.h new file mode 100644 index 0000000..95c0846 --- /dev/null +++ b/include/except.h @@ -0,0 +1,81 @@ +#ifndef EXCEPT_INCLUDED +#define EXCEPT_INCLUDED + +#include + +struct Exception { + const char *reason; +}; +typedef struct Exception Exception; + +struct ExceptFrame { + struct ExceptFrame *prev; // Exception Stack + jmp_buf env; // Enviroment Buffer + const char *file; // Exception File + int line; // Exception Line + const Exception *exception; // Exception Reason +}; +typedef struct ExceptFrame ExceptFrame; + +// Exception States +enum { EXCEPT_ENTERED=0, EXCEPT_RAISED, EXCEPT_HANDLED, EXCEPT_FINALIZED}; + +void except_raise(const Exception *e, const char *file,int line); // Raise exceptions + +// External declarations +extern ExceptFrame *except_stack; // Global exception stack +extern const Exception assert_failed; // Forward declaration for assert. + +#undef ASSERT +#ifdef NDEBUG +#define ASSERT(e) ((void)0) +#else +extern void assert(int e); +#define ASSERT(e) ((void)((e)||(RAISE(assert_failed),0))) +#endif + +// Raise an Exception. +#define RAISE(e) except_raise(&(e), __FILE__, __LINE__) + +// Reraise the currect exception. +#define RERAISE except_raise(except_frame.exception, \ + except_frame.file, except_frame.line) + +// Switch to the previous exception frame and return. +#define RETURN switch (except_stack = except_stack->prev,0) default: return + +// Start a try block. +#define TRY do { \ + volatile int except_flag; \ + ExceptFrame except_frame; \ + except_frame.prev = except_stack; \ + except_stack = &except_frame; \ + except_flag = setjmp(except_frame.env); \ + if (except_flag == EXCEPT_ENTERED) { + +// Handle specific example. +#define EXCEPT(e) \ + if (except_flag == EXCEPT_ENTERED) except_stack = except_stack->prev; \ + } else if (except_frame.exception == &(e)) { \ + except_flag = EXCEPT_HANDLED; + +// Catch all other exceptions. +#define ELSE \ + if (except_flag == EXCEPT_ENTERED) except_stack = except_stack->prev; \ + } else { \ + except_flag = EXCEPT_HANDLED; + +// Execute finalization code. +#define FINALLY \ + if (except_flag == EXCEPT_ENTERED) except_stack = except_stack->prev; \ + } { \ + if (except_flag == EXCEPT_ENTERED) \ + except_flag = EXCEPT_FINALIZED; + +// End Try block. +#define END_TRY \ + if (except_flag == EXCEPT_ENTERED) except_stack = except_stack->prev; \ + } if (except_flag == EXCEPT_RAISED) RERAISE; \ +} while (0) + +#endif diff --git a/src/except.c b/src/except.c new file mode 100644 index 0000000..ba8a1d8 --- /dev/null +++ b/src/except.c @@ -0,0 +1,29 @@ +#include "../include/except.h" +#include +#include + +ExceptFrame *except_stack = NULL; // Global exception stack. +const Exception assert_failed = { "Assertion Failure!" }; // If ASSERT fails. + +void except_raise(const Exception *e, const char *file,int line) { + // An exception was raised, grab the exception stack. + ExceptFrame *p = except_stack; + assert(e != NULL); // Ensure exception pointer is not NULL + if (p == NULL) { // Uncaught Exception + const char *msg = e->reason ? e->reason : "Uncaught Exception!"; + fprintf(stderr,"\033[31m%s | Address: 0x%p | Raised at %s@%d \033[0m" ,msg,(void *)e,file,line); + fflush(stderr); + abort(); + } + // Set the exception details to the current frame. + p->exception = e; // Exception reason + p->file = file; + p->line = line; + // Move to the previous frame in the stack. + except_stack = except_stack->prev; + // Jump to the saved context environment. + longjmp(p->env, EXCEPT_RAISED); +} +void (assert)(int e) { + ASSERT(e); +} diff --git a/tests/01_sanity.c b/tests/01_sanity.c new file mode 100644 index 0000000..97b676b --- /dev/null +++ b/tests/01_sanity.c @@ -0,0 +1,13 @@ +#define DEBUG +#include "../include/except.h" + +#include +#include + +int main(void){ + printf("\n\n"); + printf("Assertion sanity test.\n"); + ASSERT(NULL); + return EXIT_SUCCESS; + +} diff --git a/tests/02_uncaught.c b/tests/02_uncaught.c new file mode 100644 index 0000000..a3e722f --- /dev/null +++ b/tests/02_uncaught.c @@ -0,0 +1,14 @@ +#define DEBUG +#include "../include/except.h" + +#include +#include + +int main(void){ + printf("\n\n"); + printf("Uncaught exceptions sanity test.\n"); + Exception u = {NULL}; + RAISE(u); + return EXIT_SUCCESS; + +}