diff --git a/.clang-format b/.clang-format index cede7d5..9a51804 100644 --- a/.clang-format +++ b/.clang-format @@ -6,5 +6,13 @@ BreakBeforeBraces: Attach AllowShortIfStatementsOnASingleLine: false AllowShortLoopsOnASingleLine: false AllowShortFunctionsOnASingleLine: Inline -ColumnLimit: 120 +ColumnLimit: 100 AlignEscapedNewlines: Left +AlignConsecutiveDeclarations: + Enabled: true + AcrossEmptyLines: false + AcrossComments: false +AlignConsecutiveAssignments: + Enabled: true + AcrossEmptyLines: false + AcrossComments: false diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..c392697 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Debug tl_app_test", + "type": "lldb", + "request": "launch", + "program": "${workspaceFolder}/build/bin/tl_app_test", + "args": [], + "cwd": "${workspaceFolder}", + "preLaunchTask": "CMake: Build" + } + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json index 69aa14d..f0b5828 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -24,6 +24,7 @@ "ctest", "Dryrun", "eabi", + "endforeach", "libnewlib", "noninteractive", "tinyclib", diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..0323efe --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,21 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "CMake: Build", + "type": "shell", + "command": "cmake", + "args": [ + "--build", + "${workspaceFolder}/build", + "--config", + "Debug" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "problemMatcher": ["$gcc"] + } + ] +} diff --git a/CMakeLists.txt b/CMakeLists.txt index 6a4a09b..118f227 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,6 +81,13 @@ set_target_properties(tinyclib PROPERTIES POSITION_INDEPENDENT_CODE ON) if(BUILD_TESTS) include(CTest) include(FetchContent) + set(TEST_TARGETS + tl_app_test + tl_config_test + tl_debug_test + tl_error_test + tl_test_test + ) # FetchContent for Unity testing framework FetchContent_Declare( @@ -97,9 +104,9 @@ if(BUILD_TESTS) target_include_directories(Unity PUBLIC ${unity_SOURCE_DIR}/src) enable_testing() - foreach(t app config debug error test) - add_executable(tl_${t}_test tests/unit/tl_${t}_test.c) - target_link_libraries(tl_${t}_test unity tinyclib) - add_test(NAME tl_${t}_test COMMAND tl_${t}_test) + foreach(t IN LISTS TEST_TARGETS) + add_executable(${t} tests/unit/${t}.c) + target_link_libraries(${t} unity tinyclib) + add_test(NAME ${t} COMMAND ${t}) endforeach() endif() diff --git a/Makefile b/Makefile index f25d1a4..14d2e9c 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: help install build clean test format lint check check-all fix +.DEFAULT_GOAL := help BUILD_DIR := build CLANG_FORMAT := $(shell if command -v clang-format >/dev/null 2>&1; then echo clang-format; fi) @@ -8,11 +8,16 @@ CLANG_TIDY_EXTRA_ARGS := $(shell if [ "$$(uname)" = "Darwin" ]; then echo "--ext SRC_FILES := src/*.c include/*.h TEST_FILES := tests/unit/*.c -ALL_FILES := $(SRC_FILES) $(TEST_FILES) +EXAMPLE_FILES := $(wildcard examples/*/*.c) +ALL_FILES := $(SRC_FILES) $(TEST_FILES) $(EXAMPLE_FILES) + +.PHONY: help install build clean clean-bin test format lint check check-all fix help: ## Show available make targets - @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \ - awk 'BEGIN {FS = ":.*?## "}; {printf " %-15s %s\n", $$1, $$2}' + @echo "Usage: make " + @echo "" + @echo "Targets:" + @awk 'BEGIN {FS = ":.*## "} /^[a-zA-Z_-]+:.*## / {printf " %-15s %s\n", $$1, $$2}' $(MAKEFILE_LIST) configure: ## Configure cmake cmake --preset default @@ -25,6 +30,11 @@ clean: ## Remove build directory @test -n "$(CURDIR)" && [ "$(CURDIR)" != "/" ] rm -rf "$(CURDIR)/$(BUILD_DIR)" +clean-bin: ## Remove built binaries from the build directory + @test -n "$(CURDIR)" && [ "$(CURDIR)" != "/" ] + @test -d "$(CURDIR)/$(BUILD_DIR)/bin" || exit 0 + find "$(CURDIR)/$(BUILD_DIR)/bin" -mindepth 1 -delete + test: ## Run tests ctest --preset default @@ -40,8 +50,8 @@ lint: ## Check code linting check: ## Static analysis @test -n "$(CPPCHECK)" || { echo "error: cppcheck not found"; exit 1; } $(CPPCHECK) --enable=warning,style,performance,portability --error-exitcode=1 \ - --project=$(BUILD_DIR)/compile_commands.json --suppress=missingIncludeSystem \ - -i$(BUILD_DIR) + --check-level=exhaustive --project=$(BUILD_DIR)/compile_commands.json \ + --suppress=missingIncludeSystem -i$(BUILD_DIR) check-all: format lint check ## Run all checks diff --git a/README.md b/README.md index 92460fb..f1c666f 100644 --- a/README.md +++ b/README.md @@ -17,15 +17,12 @@ See [mise.toml](mise.toml) for exact versions used. - [C/C++ for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) - [CMake Tools for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cmake-tools) -## Installation +## Build ```shell -make configure make build ``` -## Usage - ## Test ```shell diff --git a/include/tl_debug.h b/include/tl_debug.h index e917b71..62f707f 100644 --- a/include/tl_debug.h +++ b/include/tl_debug.h @@ -7,7 +7,8 @@ #include /** - * @brief Prints the given message to stderr if the given debug level is greater or equal to the current debug level. + * @brief Prints the given message to stderr if the given debug level is greater or equal to the + * current debug level. * * @param level The debug level. * @param fmt The format string. diff --git a/include/tl_error.h b/include/tl_error.h index c4bbdba..9582644 100644 --- a/include/tl_error.h +++ b/include/tl_error.h @@ -9,30 +9,27 @@ * @brief Represents an error code. */ typedef enum { - TL_ERROR_NONE = 0, // no error - - TL_ERROR_INTERNAL = 10, // internal error - TL_ERROR_NOT_FOUND = 11, // not found - TL_ERROR_NOT_READY = 12, // not ready - TL_ERROR_NOT_IMPLEMENTED = 13, // not implemented - TL_ERROR_NOT_SUPPORTED = 14, // not supported - TL_ERROR_NOT_AVAILABLE = 15, // not available - - TL_ERROR_TIMEOUT = 20, // timeout error - TL_ERROR_BUSY = 21, // busy - TL_ERROR_IO = 22, // I/O error - TL_ERROR_OUT_OF_RANGE = 23, // out of range error - TL_ERROR_MEMORY_ALLOCATION = 24, // memory allocation error - - TL_ERROR_INIT_FAILED = 30, // initialization error + TL_ERROR_NONE = 0, // no error + TL_ERROR_INTERNAL = 10, // internal error + TL_ERROR_NOT_FOUND = 11, // not found + TL_ERROR_NOT_READY = 12, // not ready + TL_ERROR_NOT_IMPLEMENTED = 13, // not implemented + TL_ERROR_NOT_SUPPORTED = 14, // not supported + TL_ERROR_NOT_AVAILABLE = 15, // not available + TL_ERROR_TIMEOUT = 20, // timeout error + TL_ERROR_BUSY = 21, // busy + TL_ERROR_IO = 22, // I/O error + TL_ERROR_OUT_OF_RANGE = 23, // out of range error + TL_ERROR_MEMORY_ALLOCATION = 24, // memory allocation error + TL_ERROR_INIT_FAILED = 30, // initialization error TL_ERROR_ALREADY_INITIALIZED = 31, // already initialized - TL_ERROR_NOT_INITIALIZED = 32, // not initialized - TL_ERROR_INVALID_ARGUMENT = 33, // invalid argument - TL_ERROR_INVALID_FUNCTION = 34, // invalid function - TL_ERROR_INVALID_INSTANCE = 35, // invalid instance - TL_ERROR_INVALID_SIZE = 36, // invalid size - TL_ERROR_INVALID_TYPE = 37, // invalid type - TL_ERROR_INVALID_VALUE = 38, // invalid value + TL_ERROR_NOT_INITIALIZED = 32, // not initialized + TL_ERROR_INVALID_ARGUMENT = 33, // invalid argument + TL_ERROR_INVALID_FUNCTION = 34, // invalid function + TL_ERROR_INVALID_INSTANCE = 35, // invalid instance + TL_ERROR_INVALID_SIZE = 36, // invalid size + TL_ERROR_INVALID_TYPE = 37, // invalid type + TL_ERROR_INVALID_VALUE = 38, // invalid value } TLErrorCode; /** @@ -44,7 +41,7 @@ typedef enum { */ typedef struct { TLErrorCode code; - size_t message_size; + size_t message_size; const char *message; } TLError; diff --git a/src/tl_app.c b/src/tl_app.c index 46fd3c0..498bd00 100644 --- a/src/tl_app.c +++ b/src/tl_app.c @@ -7,8 +7,8 @@ #include // Init vars -static int arg_count = 0; -static char **args = NULL; +static int arg_count = 0; +static char **args = NULL; void tl_init_app(int argc, char *argv[]) { tl_parse_args(argc, argv); @@ -19,7 +19,7 @@ void tl_init_app(int argc, char *argv[]) { void tl_parse_args(int argc, char *argv[]) { arg_count = argc; - args = argv; + args = argv; } bool tl_lookup_flag(const char *flag) { diff --git a/src/tl_error.c b/src/tl_error.c index 59fb3f1..30a3f1a 100644 --- a/src/tl_error.c +++ b/src/tl_error.c @@ -17,7 +17,7 @@ void tl_error_set(TLError *error, TLErrorCode code, const char *message, ...) { // Clear previous error message if (error->message) { free((void *)error->message); - error->message = NULL; + error->message = NULL; error->message_size = 0; } diff --git a/tests/unit/tl_test_test.c b/tests/unit/tl_test_test.c index 005493c..c31626f 100644 --- a/tests/unit/tl_test_test.c +++ b/tests/unit/tl_test_test.c @@ -12,7 +12,7 @@ void tearDown(void) { static void test_tl_timespec_diff_ns(void) { struct timespec start = {.tv_sec = 1, .tv_nsec = 500000000}; - struct timespec end = {.tv_sec = 2, .tv_nsec = 200000000}; + struct timespec end = {.tv_sec = 2, .tv_nsec = 200000000}; long long diff_ns = tl_timespec_diff_ns(&start, &end);