feat: hectic C: debug constructor impruve

This commit is contained in:
2025-04-12 01:07:20 +00:00
parent d7bdc0722f
commit 2cb814f054
3 changed files with 56 additions and 47 deletions

View File

@@ -19,33 +19,33 @@ struct TestStruct2 {
TestStruct *other;
};
#define test_struct_to_debug_str(arena, name, self) test_struct_to_debug_str__(arena, name, self, ptrset_init(arena))
char *test_struct_to_debug_str(Arena *arena, char *name, TestStruct *self, PtrSet *visited) {
raise_trace("test_struct_to_debug_str: name: %s, self: %p, visited: %p", name, self, visited);
char *test_struct_to_debug_str__(Arena *arena, char *name, TestStruct *self, PtrSet *visited) {
if (name == NULL) {
name = "$1";
}
DEBUG_CHECK_CYCLE(arena, TestStruct, name, self, visited);
char *result = STRUCT_TO_DEBUG_STR(arena, TestStruct, name, self, 3,
char *result = arena_alloc(arena, MEM_KiB);
STRUCT_TO_DEBUG_STR(arena, result, TestStruct, name, self, visited, 3,
NUMBER_TO_DEBUG_STR(arena, "a", self->a),
NUMBER_TO_DEBUG_STR(arena, "b", self->b),
test_struct_to_debug_str__(arena, "next", self->next, visited)
test_struct_to_debug_str(arena, "next", self->next, visited)
);
return result;
}
//char *test_struct_to_debug_str__(Arena *arena, char *name, char *type, TestStruct *self) {
// char *result = arena_strdup_fmt(arena, "%s %s{", name, type);
//
// result = arena_strdup_fmt(arena, "%s%s", result, NUMBER_TO_DEBUG_STR(arena, "a", self->a));
// result = arena_strdup_fmt(arena, "%s%s", result, NUMBER_TO_DEBUG_STR(arena, "b", self->b));
// result = arena_strdup_fmt(arena, "%s%s", result, test_struct_to_debug_str__(arena, "next", TestStruct, self->next));
//
// result = arena_strdup_fmt(arena, "%s} (%p)", result, self);
// return result;
//}
char *test_struct2_to_debug_str(Arena *arena, char *name, TestStruct2 *self, PtrSet *visited) {
raise_trace("test_struct2_to_debug_str: name: %s, self: %p, visited: %p", name, self, visited);
char *result = arena_alloc(arena, MEM_KiB);
STRUCT_TO_DEBUG_STR(arena, result, TestStruct, name, self, visited, 5,
NUMBER_TO_DEBUG_STR(arena, "a", self->a),
NUMBER_TO_DEBUG_STR(arena, "f", self->f),
STRING_TO_DEBUG_STR(arena, "c", self->c),
test_struct_to_debug_str(arena, "other", self->other, visited),
test_struct2_to_debug_str(arena, "left", self->left, visited)
);
raise_trace("returning result");
return result;
}
int main(void) {
printf("%sRunning %s%s%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_CYAN), __FILE__, OPTIONAL_COLOR(COLOR_RESET));
@@ -53,8 +53,16 @@ int main(void) {
TestStruct test_struct = {.a = 1, .b = 2, .next = NULL};
test_struct.next = &test_struct;
raise_notice("%s", test_struct_to_debug_str(DISPOSABLE_ARENA, "test_struct", &test_struct));
Arena lifetime = arena_init(MEM_MiB);
PtrSet *visited = ptrset_init(&lifetime);
raise_notice("%s", test_struct_to_debug_str(&lifetime, "test_struct", &test_struct, visited));
TestStruct2 test_struct2 = {.a = 1, .c = "hello", .f = 3.14, .left = NULL, .other = &test_struct};
visited = ptrset_init(&lifetime);
raise_notice("%s", test_struct2_to_debug_str(&lifetime, "test_struct2", &test_struct2, visited));
arena_free(&lifetime);
printf("%sAll tests passed %s%s%s\n", OPTIONAL_COLOR(COLOR_GREEN), OPTIONAL_COLOR(COLOR_CYAN), __FILE__, OPTIONAL_COLOR(COLOR_RESET));
return 0;
}