From 4b3ac9ee92da9a85123d592665b1422ed376ac9f Mon Sep 17 00:00:00 2001 From: thradams Date: Sat, 23 Mar 2024 17:49:45 -0300 Subject: [PATCH] preparation --- src/file.c | 14 +++++++--- src/flow_visit.c | 29 ++++++++++++++------ src/flow_visit.h | 2 ++ src/lib.c | 70 ++++++++++++++++++++++++++++++++++++++++++------ src/object.c | 32 ++++++++++++++++++++++ src/options.h | 1 + src/type.c | 5 ++++ src/type.h | 1 + 8 files changed, 135 insertions(+), 19 deletions(-) diff --git a/src/file.c b/src/file.c index 432f8439..6b23e10f 100644 --- a/src/file.c +++ b/src/file.c @@ -1,5 +1,13 @@ -void f(_Out int i); +struct X {int i;}; + +void f(struct X * p){} + +struct X * make(){ + struct X * p = 0; + return p; +} + int main() { - int i; - f(i); + //struct X * p = 0;//make(); + //f(p); } diff --git a/src/flow_visit.c b/src/flow_visit.c index 1fba04b6..1891258e 100644 --- a/src/flow_visit.c +++ b/src/flow_visit.c @@ -1459,9 +1459,9 @@ static int compare_function_arguments3(struct parser_ctx* ctx, p_current_argument->expression->first_token, ASSIGMENT_TYPE_PARAMETER, true, - &p_current_parameter_type->type, + &p_current_parameter_type->type, ¶meter_object, /*dest object*/ - + &p_current_argument->expression->type, p_argument_object ); @@ -1605,7 +1605,7 @@ static void flow_visit_expression(struct flow_visit_ctx* ctx, struct expression* flow_visit_expression(ctx, p_expression->left); flow_visit_argument_expression_list(ctx, &p_expression->argument_expression_list); -#if 1 +#ifndef NEW_FLOW_ANALYSIS //current works compare_function_arguments2(ctx->ctx, &p_expression->left->type, &p_expression->argument_expression_list); #else @@ -2166,9 +2166,7 @@ static void flow_visit_jump_statement(struct flow_visit_ctx* ctx, struct jump_st { struct object temp_obj = { 0 }; struct object* p_object = expression_get_object(p_jump_statement->expression_opt, &temp_obj); - bool bool_source_zero_value = constant_value_is_valid(&p_jump_statement->expression_opt->constant_value) && - constant_value_to_ull(&p_jump_statement->expression_opt->constant_value) == 0; - + checked_read_object(ctx->ctx, &p_jump_statement->expression_opt->type, @@ -2178,6 +2176,10 @@ static void flow_visit_jump_statement(struct flow_visit_ctx* ctx, struct jump_st struct object dest_object = make_object(ctx->p_return_type, NULL, p_jump_statement->expression_opt); +#ifndef NEW_FLOW_ANALYSIS + bool bool_source_zero_value = constant_value_is_valid(&p_jump_statement->expression_opt->constant_value) && + constant_value_to_ull(&p_jump_statement->expression_opt->constant_value) == 0; + object_assignment(ctx->ctx, p_object, /*source*/ @@ -2189,6 +2191,17 @@ static void flow_visit_jump_statement(struct flow_visit_ctx* ctx, struct jump_st OBJECT_STATE_UNINITIALIZED, ASSIGMENT_TYPE_RETURN); +#else + object_assignment3(ctx->ctx, + p_jump_statement->expression_opt->first_token, + ASSIGMENT_TYPE_RETURN, + true, + ctx->p_return_type, /*dest type*/ + &dest_object, /*dest object*/ + &p_jump_statement->expression_opt->type, /*source type*/ + p_object /*source*/ + ); +#endif object_destroy(&dest_object); object_destroy(&temp_obj); } @@ -2584,10 +2597,10 @@ static void flow_visit_declarator(struct flow_visit_ctx* ctx, struct declarator* set_object(&t2, p_declarator->object.pointed, (OBJECT_STATE_NOT_NULL | OBJECT_STATE_NULL)); } type_destroy(&t2); - } + } #endif + } } -} /*if (p_declarator->pointer) { diff --git a/src/flow_visit.h b/src/flow_visit.h index 9f00f650..7456fb19 100644 --- a/src/flow_visit.h +++ b/src/flow_visit.h @@ -1,6 +1,8 @@ #pragma once #include "parser.h" +//#define NEW_FLOW_ANALYSIS 1 + /* To be able to do static analysis with goto jump, we need to see full function AST because this affects for diff --git a/src/lib.c b/src/lib.c index ebabec64..594cb546 100644 --- a/src/lib.c +++ b/src/lib.c @@ -595,6 +595,7 @@ enum diagnostic_id { W_OWNERSHIP_DISCARDING_OWNER, W_OWNERSHIP_NON_OWNER_MOVE, + /*ownership flow analysis errors*/ W_OWNERSHIP_FLOW_MISSING_DTOR, W_OWNERSHIP_FLOW_UNINITIALIZED, @@ -10739,6 +10740,7 @@ bool type_is_array(const struct type* p_type); bool type_is_out(const struct type* p_type); bool type_is_const(const struct type* p_type); +bool type_is_opt(const struct type* p_type); bool type_is_owner(const struct type* p_type); bool type_is_obj_owner(const struct type* p_type); bool type_is_any_owner(const struct type* p_type); @@ -18276,6 +18278,11 @@ bool type_is_owner(const struct type* p_type) return p_type->type_qualifier_flags & TYPE_QUALIFIER_OWNER; } +bool type_is_opt(const struct type* p_type) +{ + return p_type->type_qualifier_flags & TYPE_QUALIFIER_OPT; +} + bool type_is_out(const struct type* p_type) { return p_type->type_qualifier_flags & TYPE_QUALIFIER_OUT; @@ -23317,6 +23324,38 @@ void object_assignment3(struct parser_ctx* ctx, return; } + if (type_is_pointer(p_a_type) && object_is_zero_or_null(p_b_object)) + { + if (!type_is_opt(p_a_type)) + { + char buffer[100] = { 0 }; + object_get_name(p_b_type, p_b_object, buffer, sizeof buffer); + + if (assigment_type == ASSIGMENT_TYPE_PARAMETER) + { + + } + else if (assigment_type == ASSIGMENT_TYPE_RETURN) + { + compiler_diagnostic_message(W_NON_NULL, + ctx, + error_position, + "'%s' can be null, but the function result is not opt", buffer); + } + else + { + + } + + return; + } + + checked_empty(ctx, p_a_type, p_a_object, error_position); + object_set_zero(p_a_type, p_a_object); + return; + } + + type_print(p_a_type); printf(" = "); type_print(p_b_type); @@ -23596,6 +23635,8 @@ void format_visit(struct format_visit_ctx* ctx); //#pragma once +//#define NEW_FLOW_ANALYSIS 1 + /* To be able to do static analysis with goto jump, we need to see full function AST because this affects for @@ -35681,9 +35722,9 @@ static int compare_function_arguments3(struct parser_ctx* ctx, p_current_argument->expression->first_token, ASSIGMENT_TYPE_PARAMETER, true, - &p_current_parameter_type->type, + &p_current_parameter_type->type, ¶meter_object, /*dest object*/ - + &p_current_argument->expression->type, p_argument_object ); @@ -35827,7 +35868,7 @@ static void flow_visit_expression(struct flow_visit_ctx* ctx, struct expression* flow_visit_expression(ctx, p_expression->left); flow_visit_argument_expression_list(ctx, &p_expression->argument_expression_list); -#if 1 +#ifndef NEW_FLOW_ANALYSIS //current works compare_function_arguments2(ctx->ctx, &p_expression->left->type, &p_expression->argument_expression_list); #else @@ -36388,9 +36429,7 @@ static void flow_visit_jump_statement(struct flow_visit_ctx* ctx, struct jump_st { struct object temp_obj = { 0 }; struct object* p_object = expression_get_object(p_jump_statement->expression_opt, &temp_obj); - bool bool_source_zero_value = constant_value_is_valid(&p_jump_statement->expression_opt->constant_value) && - constant_value_to_ull(&p_jump_statement->expression_opt->constant_value) == 0; - + checked_read_object(ctx->ctx, &p_jump_statement->expression_opt->type, @@ -36400,6 +36439,10 @@ static void flow_visit_jump_statement(struct flow_visit_ctx* ctx, struct jump_st struct object dest_object = make_object(ctx->p_return_type, NULL, p_jump_statement->expression_opt); +#ifndef NEW_FLOW_ANALYSIS + bool bool_source_zero_value = constant_value_is_valid(&p_jump_statement->expression_opt->constant_value) && + constant_value_to_ull(&p_jump_statement->expression_opt->constant_value) == 0; + object_assignment(ctx->ctx, p_object, /*source*/ @@ -36411,6 +36454,17 @@ static void flow_visit_jump_statement(struct flow_visit_ctx* ctx, struct jump_st OBJECT_STATE_UNINITIALIZED, ASSIGMENT_TYPE_RETURN); +#else + object_assignment3(ctx->ctx, + p_jump_statement->expression_opt->first_token, + ASSIGMENT_TYPE_RETURN, + true, + ctx->p_return_type, /*dest type*/ + &dest_object, /*dest object*/ + &p_jump_statement->expression_opt->type, /*source type*/ + p_object /*source*/ + ); +#endif object_destroy(&dest_object); object_destroy(&temp_obj); } @@ -36806,10 +36860,10 @@ static void flow_visit_declarator(struct flow_visit_ctx* ctx, struct declarator* set_object(&t2, p_declarator->object.pointed, (OBJECT_STATE_NOT_NULL | OBJECT_STATE_NULL)); } type_destroy(&t2); - } + } #endif + } } -} /*if (p_declarator->pointer) { diff --git a/src/object.c b/src/object.c index 79ac783e..9c1ad3ae 100644 --- a/src/object.c +++ b/src/object.c @@ -2386,6 +2386,38 @@ void object_assignment3(struct parser_ctx* ctx, return; } + if (type_is_pointer(p_a_type) && object_is_zero_or_null(p_b_object)) + { + if (!type_is_opt(p_a_type)) + { + char buffer[100] = { 0 }; + object_get_name(p_b_type, p_b_object, buffer, sizeof buffer); + + if (assigment_type == ASSIGMENT_TYPE_PARAMETER) + { + + } + else if (assigment_type == ASSIGMENT_TYPE_RETURN) + { + compiler_diagnostic_message(W_NON_NULL, + ctx, + error_position, + "'%s' can be null, but the function result is not opt", buffer); + } + else + { + + } + + return; + } + + checked_empty(ctx, p_a_type, p_a_object, error_position); + object_set_zero(p_a_type, p_a_object); + return; + } + + type_print(p_a_type); printf(" = "); type_print(p_b_type); diff --git a/src/options.h b/src/options.h index b5d3eee8..f445f556 100644 --- a/src/options.h +++ b/src/options.h @@ -48,6 +48,7 @@ enum diagnostic_id { W_OWNERSHIP_DISCARDING_OWNER, W_OWNERSHIP_NON_OWNER_MOVE, + /*ownership flow analysis errors*/ W_OWNERSHIP_FLOW_MISSING_DTOR, W_OWNERSHIP_FLOW_UNINITIALIZED, diff --git a/src/type.c b/src/type.c index d7d3ad19..bf778dbc 100644 --- a/src/type.c +++ b/src/type.c @@ -604,6 +604,11 @@ bool type_is_owner(const struct type* p_type) return p_type->type_qualifier_flags & TYPE_QUALIFIER_OWNER; } +bool type_is_opt(const struct type* p_type) +{ + return p_type->type_qualifier_flags & TYPE_QUALIFIER_OPT; +} + bool type_is_out(const struct type* p_type) { return p_type->type_qualifier_flags & TYPE_QUALIFIER_OUT; diff --git a/src/type.h b/src/type.h index 142ee6a3..81a0c56f 100644 --- a/src/type.h +++ b/src/type.h @@ -230,6 +230,7 @@ bool type_is_array(const struct type* p_type); bool type_is_out(const struct type* p_type); bool type_is_const(const struct type* p_type); +bool type_is_opt(const struct type* p_type); bool type_is_owner(const struct type* p_type); bool type_is_obj_owner(const struct type* p_type); bool type_is_any_owner(const struct type* p_type);