Skip to content

Commit d4a0f7d

Browse files
authored
Improve debugging and formatting (#7)
1 parent 72fb7ef commit d4a0f7d

12 files changed

Lines changed: 101 additions & 45 deletions

File tree

.clang-format

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,13 @@ BreakBeforeBraces: Attach
66
AllowShortIfStatementsOnASingleLine: false
77
AllowShortLoopsOnASingleLine: false
88
AllowShortFunctionsOnASingleLine: Inline
9-
ColumnLimit: 120
9+
ColumnLimit: 100
1010
AlignEscapedNewlines: Left
11+
AlignConsecutiveDeclarations:
12+
Enabled: true
13+
AcrossEmptyLines: false
14+
AcrossComments: false
15+
AlignConsecutiveAssignments:
16+
Enabled: true
17+
AcrossEmptyLines: false
18+
AcrossComments: false

.vscode/launch.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Debug tl_app_test",
6+
"type": "lldb",
7+
"request": "launch",
8+
"program": "${workspaceFolder}/build/bin/tl_app_test",
9+
"args": [],
10+
"cwd": "${workspaceFolder}",
11+
"preLaunchTask": "CMake: Build"
12+
}
13+
]
14+
}

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"ctest",
2525
"Dryrun",
2626
"eabi",
27+
"endforeach",
2728
"libnewlib",
2829
"noninteractive",
2930
"tinyclib",

.vscode/tasks.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "CMake: Build",
6+
"type": "shell",
7+
"command": "cmake",
8+
"args": [
9+
"--build",
10+
"${workspaceFolder}/build",
11+
"--config",
12+
"Debug"
13+
],
14+
"group": {
15+
"kind": "build",
16+
"isDefault": true
17+
},
18+
"problemMatcher": ["$gcc"]
19+
}
20+
]
21+
}

CMakeLists.txt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ set_target_properties(tinyclib PROPERTIES POSITION_INDEPENDENT_CODE ON)
8181
if(BUILD_TESTS)
8282
include(CTest)
8383
include(FetchContent)
84+
set(TEST_TARGETS
85+
tl_app_test
86+
tl_config_test
87+
tl_debug_test
88+
tl_error_test
89+
tl_test_test
90+
)
8491

8592
# FetchContent for Unity testing framework
8693
FetchContent_Declare(
@@ -97,9 +104,9 @@ if(BUILD_TESTS)
97104
target_include_directories(Unity PUBLIC ${unity_SOURCE_DIR}/src)
98105

99106
enable_testing()
100-
foreach(t app config debug error test)
101-
add_executable(tl_${t}_test tests/unit/tl_${t}_test.c)
102-
target_link_libraries(tl_${t}_test unity tinyclib)
103-
add_test(NAME tl_${t}_test COMMAND tl_${t}_test)
107+
foreach(t IN LISTS TEST_TARGETS)
108+
add_executable(${t} tests/unit/${t}.c)
109+
target_link_libraries(${t} unity tinyclib)
110+
add_test(NAME ${t} COMMAND ${t})
104111
endforeach()
105112
endif()

Makefile

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: help install build clean test format lint check check-all fix
1+
.DEFAULT_GOAL := help
22

33
BUILD_DIR := build
44
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
88

99
SRC_FILES := src/*.c include/*.h
1010
TEST_FILES := tests/unit/*.c
11-
ALL_FILES := $(SRC_FILES) $(TEST_FILES)
11+
EXAMPLE_FILES := $(wildcard examples/*/*.c)
12+
ALL_FILES := $(SRC_FILES) $(TEST_FILES) $(EXAMPLE_FILES)
13+
14+
.PHONY: help install build clean clean-bin test format lint check check-all fix
1215

1316
help: ## Show available make targets
14-
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
15-
awk 'BEGIN {FS = ":.*?## "}; {printf " %-15s %s\n", $$1, $$2}'
17+
@echo "Usage: make <target>"
18+
@echo ""
19+
@echo "Targets:"
20+
@awk 'BEGIN {FS = ":.*## "} /^[a-zA-Z_-]+:.*## / {printf " %-15s %s\n", $$1, $$2}' $(MAKEFILE_LIST)
1621

1722
configure: ## Configure cmake
1823
cmake --preset default
@@ -25,6 +30,11 @@ clean: ## Remove build directory
2530
@test -n "$(CURDIR)" && [ "$(CURDIR)" != "/" ]
2631
rm -rf "$(CURDIR)/$(BUILD_DIR)"
2732

33+
clean-bin: ## Remove built binaries from the build directory
34+
@test -n "$(CURDIR)" && [ "$(CURDIR)" != "/" ]
35+
@test -d "$(CURDIR)/$(BUILD_DIR)/bin" || exit 0
36+
find "$(CURDIR)/$(BUILD_DIR)/bin" -mindepth 1 -delete
37+
2838
test: ## Run tests
2939
ctest --preset default
3040

@@ -40,8 +50,8 @@ lint: ## Check code linting
4050
check: ## Static analysis
4151
@test -n "$(CPPCHECK)" || { echo "error: cppcheck not found"; exit 1; }
4252
$(CPPCHECK) --enable=warning,style,performance,portability --error-exitcode=1 \
43-
--project=$(BUILD_DIR)/compile_commands.json --suppress=missingIncludeSystem \
44-
-i$(BUILD_DIR)
53+
--check-level=exhaustive --project=$(BUILD_DIR)/compile_commands.json \
54+
--suppress=missingIncludeSystem -i$(BUILD_DIR)
4555

4656
check-all: format lint check ## Run all checks
4757

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@ See [mise.toml](mise.toml) for exact versions used.
1717
- [C/C++ for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools)
1818
- [CMake Tools for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cmake-tools)
1919

20-
## Installation
20+
## Build
2121

2222
```shell
23-
make configure
2423
make build
2524
```
2625

27-
## Usage
28-
2926
## Test
3027

3128
```shell

include/tl_debug.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
#include <stdio.h>
88

99
/**
10-
* @brief Prints the given message to stderr if the given debug level is greater or equal to the current debug level.
10+
* @brief Prints the given message to stderr if the given debug level is greater or equal to the
11+
* current debug level.
1112
*
1213
* @param level The debug level.
1314
* @param fmt The format string.

include/tl_error.h

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,27 @@
99
* @brief Represents an error code.
1010
*/
1111
typedef enum {
12-
TL_ERROR_NONE = 0, // no error
13-
14-
TL_ERROR_INTERNAL = 10, // internal error
15-
TL_ERROR_NOT_FOUND = 11, // not found
16-
TL_ERROR_NOT_READY = 12, // not ready
17-
TL_ERROR_NOT_IMPLEMENTED = 13, // not implemented
18-
TL_ERROR_NOT_SUPPORTED = 14, // not supported
19-
TL_ERROR_NOT_AVAILABLE = 15, // not available
20-
21-
TL_ERROR_TIMEOUT = 20, // timeout error
22-
TL_ERROR_BUSY = 21, // busy
23-
TL_ERROR_IO = 22, // I/O error
24-
TL_ERROR_OUT_OF_RANGE = 23, // out of range error
25-
TL_ERROR_MEMORY_ALLOCATION = 24, // memory allocation error
26-
27-
TL_ERROR_INIT_FAILED = 30, // initialization error
12+
TL_ERROR_NONE = 0, // no error
13+
TL_ERROR_INTERNAL = 10, // internal error
14+
TL_ERROR_NOT_FOUND = 11, // not found
15+
TL_ERROR_NOT_READY = 12, // not ready
16+
TL_ERROR_NOT_IMPLEMENTED = 13, // not implemented
17+
TL_ERROR_NOT_SUPPORTED = 14, // not supported
18+
TL_ERROR_NOT_AVAILABLE = 15, // not available
19+
TL_ERROR_TIMEOUT = 20, // timeout error
20+
TL_ERROR_BUSY = 21, // busy
21+
TL_ERROR_IO = 22, // I/O error
22+
TL_ERROR_OUT_OF_RANGE = 23, // out of range error
23+
TL_ERROR_MEMORY_ALLOCATION = 24, // memory allocation error
24+
TL_ERROR_INIT_FAILED = 30, // initialization error
2825
TL_ERROR_ALREADY_INITIALIZED = 31, // already initialized
29-
TL_ERROR_NOT_INITIALIZED = 32, // not initialized
30-
TL_ERROR_INVALID_ARGUMENT = 33, // invalid argument
31-
TL_ERROR_INVALID_FUNCTION = 34, // invalid function
32-
TL_ERROR_INVALID_INSTANCE = 35, // invalid instance
33-
TL_ERROR_INVALID_SIZE = 36, // invalid size
34-
TL_ERROR_INVALID_TYPE = 37, // invalid type
35-
TL_ERROR_INVALID_VALUE = 38, // invalid value
26+
TL_ERROR_NOT_INITIALIZED = 32, // not initialized
27+
TL_ERROR_INVALID_ARGUMENT = 33, // invalid argument
28+
TL_ERROR_INVALID_FUNCTION = 34, // invalid function
29+
TL_ERROR_INVALID_INSTANCE = 35, // invalid instance
30+
TL_ERROR_INVALID_SIZE = 36, // invalid size
31+
TL_ERROR_INVALID_TYPE = 37, // invalid type
32+
TL_ERROR_INVALID_VALUE = 38, // invalid value
3633
} TLErrorCode;
3734

3835
/**
@@ -44,7 +41,7 @@ typedef enum {
4441
*/
4542
typedef struct {
4643
TLErrorCode code;
47-
size_t message_size;
44+
size_t message_size;
4845
const char *message;
4946
} TLError;
5047

src/tl_app.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#include <string.h>
88

99
// Init vars
10-
static int arg_count = 0;
11-
static char **args = NULL;
10+
static int arg_count = 0;
11+
static char **args = NULL;
1212

1313
void tl_init_app(int argc, char *argv[]) {
1414
tl_parse_args(argc, argv);
@@ -19,7 +19,7 @@ void tl_init_app(int argc, char *argv[]) {
1919

2020
void tl_parse_args(int argc, char *argv[]) {
2121
arg_count = argc;
22-
args = argv;
22+
args = argv;
2323
}
2424

2525
bool tl_lookup_flag(const char *flag) {

0 commit comments

Comments
 (0)