Skip to content

Commit

Permalink
Makefile UPDATE
Browse files Browse the repository at this point in the history
  • Loading branch information
alecksandr26 committed Jan 26, 2024
1 parent e88ccaa commit ebce13b
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 81 deletions.
35 changes: 10 additions & 25 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ define \n
endef

C = cc
C_DEBUG_FLAGS = -ggdb -pedantic -Wall
C_COMPILE_FLAGS = -O2 -DNDEBUG -fno-stack-protector -z execstack -no-pie
C_DEBUG_FLAGS = -ggdb -pedantic -Wall -fPIC
C_COMPILE_FLAGS = -O2 -DNDEBUG -fno-stack-protector -z execstack -no-pie -fPIC
C_FLAGS = $(C_DEBUG_FLAGS)
N = nasm
N_DEBUG_FLAGS = -g -f elf64
Expand Down Expand Up @@ -69,6 +69,10 @@ $(LIB_DIR):
@echo Creating: $@
@mkdir -p $@

$(UPLOAD_DIR):
@echo Creating: $@
@mkdir -p $@

# Create the output binary
$(TEST_BIN_DIR):
@echo Creating: $@
Expand Down Expand Up @@ -102,38 +106,19 @@ test_%.out: $(TEST_BIN_DIR)/test_%.out
# To Run all the tests
test: $(notdir $(TESTS))

# To clean any compiled object
clean_$(OBJ_DIR)/%.o:
@echo Removing: $(patsubst clean_%, %, $@)
@rm $(patsubst clean_%, %, $@)

# To clean any archive library
clean_$(LIB_DIR)/%.a:
@echo Removing: $(patsubst clean_%, %, $@)
@rm $(patsubst clean_%, %, $@)

# To clean any compiled test
clean_$(TEST_BIN_DIR)/%.out:
@echo Removing: $(patsubst clean_%, %, $@)
@rm $(patsubst clean_%, %, $@)

# Remove all the compiled dependencies and tes
clean: $(addprefix clean_, $(wildcard $(OBJ_DIR)/*.o) \
$(wildcard $(LIB_DIR)/*.a) \
$(wildcard $(TEST_BIN_DIR)/*.out))
clean:
ifneq ("$(wildcard $(OBJ_DIR))", "")
@echo Removing: $(OBJ_DIR)
@rmdir $(OBJ_DIR)
@rm -vr $(OBJ_DIR)
endif

ifneq ("$(wildcard $(TESTS_BIN_DIR))", "")
@echo Removing: $(TEST_BIN_DIR)
@rmdir $(TESTS_BIN_DIR)
@rm -vr $(TESTS_BIN_DIR)
endif

ifneq ("$(wildcard $(LIB_DIR))", "")
@echo Removing: $(LIB_DIR)
@rmdir $(LIB_DIR)
@rm -vr $(LIB_DIR)
endif

# Clean objects and libs and recompile with optimizations
Expand Down
4 changes: 2 additions & 2 deletions PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

# Maintainer: alecksandr <sansepiol26@gmail.com>
pkgname=trycatch-c
pkgver=1.3.0
pkgrel=2
pkgver=1.3.1
pkgrel=3
epoch=
pkgdesc="This module offers a straightforward macro interface that facilitates seamless exception handling in
the C programming language, drawing inspiration from the paradigm employed in C++."
Expand Down
2 changes: 2 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---- 2024/01/26 ----
- Create like standard exceptions [ ]
35 changes: 18 additions & 17 deletions include/trycatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,38 @@ struct E {

typedef struct F F;
struct F {
F *prev;
JmpBuf contex;
F *prev;
JmpBuf contex;
const char *file;
int line;
const E *exception;
};

#define try \
do { \
#define try \
do { \
volatile int __except_flag; \
ExceptFrame __except_frame; /* Creates the except frame */ \
/* Link the frames */ \
__except_frame.prev = __except_head; \
__except_head = &__except_frame; \
__except_flag = stackjmp(&__except_frame.contex); \
/* Try something */ \
/* Link the frames */ \
__except_frame.prev = __except_head; \
__except_head = &__except_frame; \
__except_flag = stackjmp(&__except_frame.contex); \
/* Try something */ \
if (__except_flag == EXCEPT_ENTERED)
#define throw(e) __tc_except_raise(&(e), __FILE__, __LINE__)
#define RE_RAISE \
__tc_except_raise(__except_frame.exception, __except_frame.file, __except_frame.line)
#define catch(e) \
#define RE_RAISE \
__tc_except_raise(__except_frame.exception, __except_frame.file, \
__except_frame.line)
#define catch(e) \
else if (__except_frame.exception == &(e) && (__except_flag = EXCEPT_HANDLED))
#define otherwise else if ((__except_flag = EXCEPT_HANDLED))
#define endtry \
; \
#define endtry \
; \
if (__except_flag == EXCEPT_ENTERED) __except_head = __except_head->prev; \
if (__except_flag == EXCEPT_RAISED) RE_RAISE; \
} \
if (__except_flag == EXCEPT_RAISED) RE_RAISE; \
} \
while (0)

extern F *__except_head;
extern F *__except_head;
extern void __tc_except_raise(const E *e, const char *file, int line);

#undef E
Expand Down
12 changes: 5 additions & 7 deletions src/trycatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
@license This project is released under the MIT License
*/


#include "../include/trycatch.h"

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#include "../include/trycatch.h"

#define F ExceptFrame
#define E Except

Expand All @@ -27,12 +25,12 @@ void __tc_except_raise(const E *e, const char *file, int line)

F *frame = __except_head;

if (frame == NULL) {
if (frame == 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:%i\n", file, line);

fprintf(stderr, "Aborting....\n");
fflush(stderr);
abort();
Expand All @@ -42,8 +40,8 @@ void __tc_except_raise(const E *e, const char *file, int line)
frame->exception = e;
frame->file = file;
frame->line = line;
__except_head = __except_head->prev;
__except_head = __except_head->prev;

jmpback(&frame->contex, EXCEPT_RAISED);
}

Expand Down
2 changes: 1 addition & 1 deletion test/src/test_stackjmp.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* TO test the setjmp */
#include "../../include/tc/stackjmp.h"
#include "../../include/trycatch/stackjmp.h"

#include <assert.h>
#include <stdio.h>
Expand Down
47 changes: 18 additions & 29 deletions test/src/test_trycatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@ void test_simple_try_except(void)
{
int i = 0;

try
throw(SomeError); /* throw_except some error */
catch(SomeError3)
i = 5;
catch(SomeError)
i++;
catch(SomeError2)
i = 3;
otherwise
i = 2;
try throw(SomeError); /* throw_except some error */
catch (SomeError3) i = 5;
catch (SomeError) i++;
catch (SomeError2) i = 3;
otherwise i = 2;
endtry;

assert(i == 1);
Expand All @@ -35,8 +30,7 @@ Except NotEnoughMemory = {"Not enough memory"},
/* always is going to return null */
void *alloc_will_be_null(int num)
{
if (num < 1)
throw(AllocNumShouldBePositive);
if (num < 1) throw(AllocNumShouldBePositive);
return NULL;
}

Expand All @@ -55,9 +49,9 @@ void test_simulate_try_except(void)
{
void *some_instace = NULL;

try
some_instace = create_instance_of_something(10);
catch(NotEnoughMemory) {
try some_instace = create_instance_of_something(10);
catch (NotEnoughMemory)
{
assert(some_instace == NULL);
assert(__except_flag == EXCEPT_HANDLED);
}
Expand All @@ -68,16 +62,12 @@ void test_multiples_trys(void)
{
int i = 0;

try
throw(SomeError);
catch(SomeError)
i++;
try throw(SomeError);
catch (SomeError) i++;
endtry;

try
throw(SomeError2);
catch(SomeError2)
i++;
try throw(SomeError2);
catch (SomeError2) i++;
endtry;

assert(i == 2);
Expand All @@ -87,9 +77,8 @@ void more_try(void)
{
void *instance = NULL;

try
instance = create_instance_of_something(-1);
catch(AllocNumShouldBePositive)
try instance = create_instance_of_something(-1);
catch (AllocNumShouldBePositive)
{
assert(instance == NULL);
throw(AllocNumShouldBePositive); /* Raise again an error */
Expand All @@ -99,9 +88,9 @@ void more_try(void)

void test_trys_inside_trys(void)
{
try
more_try();
catch(AllocNumShouldBePositive) {
try more_try();
catch (AllocNumShouldBePositive)
{
assert(exception == AllocNumShouldBePositive.reason);
printf("%s\n", exception);
}
Expand Down

0 comments on commit ebce13b

Please sign in to comment.