From 0c2c67f49b778dc857e169c814d2e90208f10944 Mon Sep 17 00:00:00 2001 From: thradams Date: Sun, 24 Mar 2024 18:15:28 -0300 Subject: [PATCH] preparation --- src/file.c | 5 ++- src/lib.c | 121 ++++++++++++++++++++++++++++----------------------- src/main.c | 2 +- src/object.c | 57 +++++++++++++++--------- src/tests.c | 3 +- 5 files changed, 107 insertions(+), 81 deletions(-) diff --git a/src/file.c b/src/file.c index 3feeb3c4..37a37115 100644 --- a/src/file.c +++ b/src/file.c @@ -8,9 +8,10 @@ void x_destroy(struct X* _Obj_owner p); void f(struct X* x) { x_destroy(x); - #pragma cake diagnostic check "-Wmust-use-address-of" + static_debug(x); } -#pragma cake diagnostic check "-Wmissing-destructor" + #pragma cake diagnostic check "-Wmust-use-address-of" + diff --git a/src/lib.c b/src/lib.c index 085ee41d..bad028cb 100644 --- a/src/lib.c +++ b/src/lib.c @@ -22755,17 +22755,22 @@ void checked_moved(struct parser_ctx* ctx, } p_member_declaration = p_member_declaration->next; } + return; } else { if (type_is_pointer(p_type) && !type_is_any_owner(p_type)) { - struct type t2 = type_remove_pointer(p_type); - checked_moved(ctx, - &t2, - object_get_pointed_object(p_object), - position_token); - type_destroy(&t2); + if (p_object->state != OBJECT_STATE_UNINITIALIZED && + p_object->state != OBJECT_STATE_NULL) + { + struct type t2 = type_remove_pointer(p_type); + checked_moved(ctx, + &t2, + object_get_pointed_object(p_object), + position_token); + type_destroy(&t2); + } } if (p_object->state & OBJECT_STATE_MOVED) @@ -23311,11 +23316,11 @@ void object_assignment3(struct parser_ctx* ctx, { return; } - //printf("line %d ", error_position->line); - //type_print(p_a_type); - //printf(" = "); - //type_print(p_b_type); - //printf("\n"); + printf("line %d ", error_position->line); + type_print(p_a_type); + printf(" = "); + type_print(p_b_type); + printf("\n"); /*general check for copying uninitialized object*/ if (check_uninitialized_b && p_b_object->state & OBJECT_STATE_UNINITIALIZED) @@ -23365,7 +23370,7 @@ void object_assignment3(struct parser_ctx* ctx, error_position, "assignment of possible null object '%s' to non-opt pointer", buffer); #endif //nullchecks disabled for now - } +} if (type_is_owner(p_a_type) && type_is_pointer(p_a_type)) { @@ -23457,20 +23462,28 @@ void object_assignment3(struct parser_ctx* ctx, } else { - if (assigment_type == ASSIGMENT_TYPE_PARAMETER) + if (p_b_type->address_of) { - p_b_object->state = OBJECT_STATE_UNINITIALIZED; - struct object* pointed = object_get_pointed_object(p_b_object); - if (pointed) + //must be address of. + if (assigment_type == ASSIGMENT_TYPE_PARAMETER) { - struct type t2 = type_remove_pointer(p_b_type); - object_set_uninitialized(&t2, pointed); - type_destroy(&t2); - } + p_b_object->state = OBJECT_STATE_UNINITIALIZED; + struct object* pointed = object_get_pointed_object(p_b_object); + if (pointed) + { + struct type t2 = type_remove_pointer(p_b_type); + object_set_uninitialized(&t2, pointed); + type_destroy(&t2); + } + } + else + object_set_moved(p_b_type, p_b_object); } else - object_set_moved(p_b_type, p_b_object); + { + //error already created. + } } } @@ -23591,7 +23604,7 @@ void format_visit(struct format_visit_ctx* ctx); //#pragma once -//#define NEW_FLOW_ANALYSIS 1 +#define NEW_FLOW_ANALYSIS 1 /* To be able to do static analysis with goto jump, we @@ -40603,39 +40616,37 @@ void use_after_destroy() void obj_owner_must_be_from_addressof() { - const char* source - = - "void free(void* _Owner ptr);\n" - "void* _Owner malloc(int size);\n" - "char* _Owner strdup(const char*);\n" - "\n" - "struct X {\n" - " char* _Owner name;\n" - "};\n" - "\n" - "struct Y {\n" - " struct X x;\n" - " struct X* px;\n" - "};\n" - "\n" - "void x_destroy(struct X* _Obj_owner p)\n" - "{\n" - " free(p->name);\n" - "}\n" - "\n" - "void f(struct Y* p)\n" - "{\n" - " x_destroy(p->px);\n" - "#pragma cake diagnostic check \"-Wmust-use-address-of\"\n" - "}\n" - "\n" - "int main() {\n" - " struct Y y = {};\n" - " struct* p = &y.x;\n" - " x_destroy(&y.x);\n" - "}\n" - "#pragma cake diagnostic check \"-Wmissing-destructor\"\n" - ""; + const char* source = + "void free(void* _Owner ptr);\n" + "void* _Owner malloc(int size);\n" + "char* _Owner strdup(const char*);\n" + "\n" + "struct X {\n" + " char* _Owner name;\n" + "};\n" + "\n" + "struct Y {\n" + " struct X x;\n" + " struct X* px;\n" + "};\n" + "\n" + "void x_destroy(struct X* _Obj_owner p)\n" + "{\n" + " free(p->name);\n" + "}\n" + "\n" + "void f(struct Y* p)\n" + "{\n" + " x_destroy(p->px);\n" + "#pragma cake diagnostic check \"-Wmust-use-address-of\"\n" + "}\n" + "\n" + "int main() {\n" + " struct Y y = {};\n" + " struct* p = &y.x;\n" + " x_destroy(&y.x);\n" + "}\n" + ""; assert(compile_without_errors_warnings(true, false, source)); diff --git a/src/main.c b/src/main.c index 16a1a305..5ef8da0d 100644 --- a/src/main.c +++ b/src/main.c @@ -64,7 +64,7 @@ int main(int argc, char** argv) } #else #include "unit_test.c" -#define CURRENT_NUMBER_OF_FAILING_TEST 3 +#define CURRENT_NUMBER_OF_FAILING_TEST 2 int main(int argc, char** argv) { diff --git a/src/object.c b/src/object.c index a380c3a1..6bd44972 100644 --- a/src/object.c +++ b/src/object.c @@ -1807,17 +1807,22 @@ void checked_moved(struct parser_ctx* ctx, } p_member_declaration = p_member_declaration->next; } + return; } else { if (type_is_pointer(p_type) && !type_is_any_owner(p_type)) { - struct type t2 = type_remove_pointer(p_type); - checked_moved(ctx, - &t2, - object_get_pointed_object(p_object), - position_token); - type_destroy(&t2); + if (p_object->state != OBJECT_STATE_UNINITIALIZED && + p_object->state != OBJECT_STATE_NULL) + { + struct type t2 = type_remove_pointer(p_type); + checked_moved(ctx, + &t2, + object_get_pointed_object(p_object), + position_token); + type_destroy(&t2); + } } if (p_object->state & OBJECT_STATE_MOVED) @@ -2363,11 +2368,11 @@ void object_assignment3(struct parser_ctx* ctx, { return; } - //printf("line %d ", error_position->line); - //type_print(p_a_type); - //printf(" = "); - //type_print(p_b_type); - //printf("\n"); + printf("line %d ", error_position->line); + type_print(p_a_type); + printf(" = "); + type_print(p_b_type); + printf("\n"); /*general check for copying uninitialized object*/ if (check_uninitialized_b && p_b_object->state & OBJECT_STATE_UNINITIALIZED) @@ -2417,7 +2422,7 @@ void object_assignment3(struct parser_ctx* ctx, error_position, "assignment of possible null object '%s' to non-opt pointer", buffer); #endif //nullchecks disabled for now - } +} if (type_is_owner(p_a_type) && type_is_pointer(p_a_type)) { @@ -2509,20 +2514,30 @@ void object_assignment3(struct parser_ctx* ctx, } else { - if (assigment_type == ASSIGMENT_TYPE_PARAMETER) + if (p_b_type->address_of) { - p_b_object->state = OBJECT_STATE_UNINITIALIZED; - struct object* pointed = object_get_pointed_object(p_b_object); - if (pointed) + //must be address of. + if (assigment_type == ASSIGMENT_TYPE_PARAMETER) { - struct type t2 = type_remove_pointer(p_b_type); - object_set_uninitialized(&t2, pointed); - type_destroy(&t2); - } + p_b_object->state = OBJECT_STATE_UNINITIALIZED; + struct object* pointed = object_get_pointed_object(p_b_object); + if (pointed) + { + struct type t2 = type_remove_pointer(p_b_type); + object_set_uninitialized(&t2, pointed); + type_destroy(&t2); + } + } + else + object_set_moved(p_b_type, p_b_object); } else - object_set_moved(p_b_type, p_b_object); + { + //avoid error on top of error + //address error already emmited + //at this point + } } } diff --git a/src/tests.c b/src/tests.c index 7197c007..39c06c10 100644 --- a/src/tests.c +++ b/src/tests.c @@ -2445,8 +2445,7 @@ void obj_owner_must_be_from_addressof() " struct Y y = {};\n" " struct* p = &y.x;\n" " x_destroy(&y.x);\n" - "}\n" - "#pragma cake diagnostic check \"-Wmissing-destructor\"\n" + "}\n" "";