Compare commits
4 Commits
akmain
...
4a09ca87fd
| Author | SHA1 | Date | |
|---|---|---|---|
|
4a09ca87fd
|
|||
|
51b6b23b4c
|
|||
|
166a478e6a
|
|||
|
0c29f5d69f
|
@@ -7,27 +7,37 @@ include(CMakePackageConfigHelpers)
|
||||
set(AKERR_USE_STDLIB 1 CACHE BOOL "Use the C standard library")
|
||||
set(akerror_install_cmakedir "${CMAKE_INSTALL_LIBDIR}/cmake/akerror")
|
||||
|
||||
set(SCRIPT ${CMAKE_SOURCE_DIR}/scripts/generrno.sh)
|
||||
set(INFILE ${CMAKE_SOURCE_DIR}/include/akerror.tmpl.h)
|
||||
set(OUTFILES ${CMAKE_SOURCE_DIR}/src/errno.c ${CMAKE_SOURCE_DIR}/include/akerror.h)
|
||||
set(SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generrno.sh)
|
||||
set(INFILE ${CMAKE_CURRENT_SOURCE_DIR}/include/akerror.tmpl.h)
|
||||
|
||||
set(GENERATED_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated)
|
||||
|
||||
set(GENERATED_ERRNO_C ${GENERATED_DIR}/src/errno.c)
|
||||
set(GENERATED_AKERROR_H ${GENERATED_DIR}/include/akerror.h)
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT ${OUTFILES}
|
||||
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_SOURCE_DIR}
|
||||
COMMAND /usr/bin/env bash ${SCRIPT} ${CMAKE_SOURCE_DIR}
|
||||
OUTPUT ${GENERATED_ERRNO_C} ${GENERATED_AKERROR_H}
|
||||
COMMAND ${CMAKE_COMMAND} -E make_directory ${GENERATED_DIR}
|
||||
COMMAND /usr/bin/env bash
|
||||
${SCRIPT}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
${GENERATED_DIR}
|
||||
DEPENDS ${SCRIPT} ${INFILE}
|
||||
VERBATIM
|
||||
)
|
||||
|
||||
set_source_files_properties(src/errno.c PROPERTIES GENERATED TRUE)
|
||||
add_library(akerror SHARED
|
||||
src/error.c
|
||||
${GENERATED_ERRNO_C}
|
||||
)
|
||||
|
||||
add_custom_target(generrno DEPENDS src/errno.c)
|
||||
target_include_directories(akerror PUBLIC
|
||||
$<BUILD_INTERFACE:${GENERATED_DIR}>/include
|
||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
||||
)
|
||||
|
||||
find_package(PkgConfig REQUIRED)
|
||||
add_library(akerror STATIC
|
||||
src/error.c
|
||||
src/errno.c
|
||||
)
|
||||
add_dependencies(akerror generrno)
|
||||
add_library(akerror::akerror ALIAS akerror)
|
||||
|
||||
target_compile_definitions(akerror
|
||||
PUBLIC AKERR_USE_STDLIB=${AKERR_USE_STDLIB}
|
||||
@@ -36,21 +46,18 @@ target_compile_definitions(akerror
|
||||
add_executable(test_err_catch tests/err_catch.c)
|
||||
add_executable(test_err_cleanup tests/err_cleanup.c)
|
||||
add_executable(test_err_trace tests/err_trace.c)
|
||||
add_executable(test_err_improper_closure tests/err_improper_closure.c)
|
||||
add_test(NAME err_catch COMMAND test_err_catch)
|
||||
add_test(NAME err_cleanup COMMAND test_err_cleanup)
|
||||
add_test(NAME err_trace COMMAND test_err_trace)
|
||||
add_test(NAME err_improper_closure COMMAND test_err_improper_closure)
|
||||
|
||||
# Specify include directories for the library's headers (if applicable)
|
||||
target_include_directories(akerror PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/>
|
||||
)
|
||||
target_link_libraries(test_err_catch PRIVATE akerror)
|
||||
target_link_libraries(test_err_cleanup PRIVATE akerror)
|
||||
target_link_libraries(test_err_trace PRIVATE akerror)
|
||||
target_link_libraries(test_err_improper_closure PRIVATE akerror)
|
||||
|
||||
set(main_lib_dest "lib/my_library-${MY_LIBRARY_VERSION}")
|
||||
install(TARGETS akerror EXPORT akerror DESTINATION "lib/")
|
||||
install(TARGETS akerror
|
||||
EXPORT akerrorTargets
|
||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
@@ -59,8 +66,6 @@ install(TARGETS akerror
|
||||
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
|
||||
)
|
||||
|
||||
install(EXPORT akerror FILE akerrorTargets.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/akerror)
|
||||
|
||||
install(FILES "include/akerror.h" DESTINATION "include/")
|
||||
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/akerror.pc DESTINATION "lib/pkgconfig/")
|
||||
|
||||
|
||||
@@ -30,9 +30,10 @@
|
||||
#define AKERR_VALUE (AKERR_LAST_ERRNO_VALUE + 12)
|
||||
#define AKERR_BEHAVIOR (AKERR_LAST_ERRNO_VALUE + 13)
|
||||
#define AKERR_RELATIONSHIP (AKERR_LAST_ERRNO_VALUE + 14)
|
||||
#define AKERR_EOF (AKERR_LAST_ERRNO_VALUE + 15)
|
||||
|
||||
#ifndef AKERR_MAX_ERR_VALUE
|
||||
#define AKERR_MAX_ERR_VALUE (AKERR_LAST_ERRNO_VALUE + 14)
|
||||
#define AKERR_MAX_ERR_VALUE (AKERR_LAST_ERRNO_VALUE + 15)
|
||||
#elif AKERR_MAX_ERR_VALUE < 256
|
||||
#error user-defined AKERR_MAX_ERR_VALUE must be >= 256
|
||||
#endif
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
#!/bin/bash
|
||||
|
||||
srcdir=$1
|
||||
rm -f ${srcdir}/src/errno.c
|
||||
echo "#include <akerror.h>" >> ${srcdir}/src/errno.c
|
||||
echo "#include <errno.h>" >> ${srcdir}/src/errno.c
|
||||
echo "void akerr_init_errno(void) {" >> ${srcdir}/src/errno.c
|
||||
outdir=$2
|
||||
|
||||
mkdir -p ${outdir}/src
|
||||
mkdir -p ${outdir}/include
|
||||
rm -f ${outdir}/src/errno.c
|
||||
echo "#include <akerror.h>" >> ${outdir}/src/errno.c
|
||||
echo "#include <errno.h>" >> ${outdir}/src/errno.c
|
||||
echo "void akerr_init_errno(void) {" >> ${outdir}/src/errno.c
|
||||
maxval=$(errno --list | cut -d ' ' -f 2 | sort -g | tail -n 1)
|
||||
errno --list | while read LINE; do
|
||||
define=$(echo "$LINE" | cut -d ' ' -f 1);
|
||||
value=$(echo "$LINE" | cut -d ' ' -f 2);
|
||||
desc=$(echo "$LINE" | cut -d ' ' -f 3-);
|
||||
echo " akerr_name_for_status(${define}, \"${desc}\");" >> ${srcdir}/src/errno.c ;
|
||||
echo " akerr_name_for_status(${define}, \"${desc}\");" >> ${outdir}/src/errno.c ;
|
||||
done;
|
||||
echo "}" >> ${srcdir}/src/errno.c
|
||||
sed "s/#define AKERR_LAST_ERRNO_VALUE .*/#define AKERR_LAST_ERRNO_VALUE ${maxval}/" ${srcdir}/include/akerror.tmpl.h > ${srcdir}/include/akerror.h
|
||||
echo "}" >> ${outdir}/src/errno.c
|
||||
sed "s/#define AKERR_LAST_ERRNO_VALUE .*/#define AKERR_LAST_ERRNO_VALUE ${maxval}/" ${srcdir}/include/akerror.tmpl.h > ${outdir}/include/akerror.h
|
||||
|
||||
10
src/error.c
10
src/error.c
@@ -14,6 +14,12 @@ char __AKERR_ERROR_NAMES[AKERR_MAX_ERR_VALUE+1][AKERR_MAX_ERROR_NAME_LENGTH];
|
||||
|
||||
akerr_ErrorContext AKERR_ARRAY_ERROR[AKERR_MAX_ARRAY_ERROR];
|
||||
|
||||
int akerr_valid_error_address(akerr_ErrorContext *ptr)
|
||||
{
|
||||
// Is this within the memory region occupied by AKERR_ARRAY_ERROR?
|
||||
return ((ptr >= &AKERR_ARRAY_ERROR[0]) && (ptr <= &AKERR_ARRAY_ERROR[AKERR_MAX_ARRAY_ERROR-1]));
|
||||
}
|
||||
|
||||
void akerr_default_logger(const char *fmt, ...)
|
||||
{
|
||||
#if defined(AKERR_USE_STDLIB) && AKERR_USE_STDLIB == 1
|
||||
@@ -59,7 +65,9 @@ void akerr_init()
|
||||
akerr_name_for_status(AKERR_VALUE, "Value Error");
|
||||
akerr_name_for_status(AKERR_BEHAVIOR, "Behavior Error");
|
||||
akerr_name_for_status(AKERR_RELATIONSHIP, "Relationship Error");
|
||||
|
||||
#if (defined(AKERR_USE_STDLIB) && AKERR_USE_STDLIB == 1) || (!defined(AKERR_USE_STDLIB))
|
||||
akerr_init_errno();
|
||||
#endif
|
||||
inited = 1;
|
||||
}
|
||||
}
|
||||
|
||||
22
tests/err_improper_closure.c
Normal file
22
tests/err_improper_closure.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "akerror.h"
|
||||
#include <stdio.h>
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *improper_closure(void)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
ATTEMPT {
|
||||
} CLEANUP {
|
||||
} PROCESS(errctx) {
|
||||
} FINISH(errctx, true);
|
||||
fprintf(stderr, "Improperly returning from improper_closure\n");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
ATTEMPT {
|
||||
CATCH(errctx, improper_closure());
|
||||
} CLEANUP {
|
||||
} PROCESS(errctx) {
|
||||
} FINISH_NORETURN(errctx);
|
||||
}
|
||||
Reference in New Issue
Block a user