Skip to content

Commit

Permalink
preparation
Browse files Browse the repository at this point in the history
  • Loading branch information
thradams committed Mar 23, 2024
1 parent 90a12e5 commit 4b3ac9e
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 19 deletions.
14 changes: 11 additions & 3 deletions src/file.c
Original file line number Diff line number Diff line change
@@ -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);
}
29 changes: 21 additions & 8 deletions src/flow_visit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
&parameter_object, /*dest object*/

&p_current_argument->expression->type,
p_argument_object
);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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*/
Expand All @@ -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);
}
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 2 additions & 0 deletions src/flow_visit.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down
70 changes: 62 additions & 8 deletions src/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
&parameter_object, /*dest object*/

&p_current_argument->expression->type,
p_argument_object
);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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*/
Expand All @@ -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);
}
Expand Down Expand Up @@ -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)
{
Expand Down
32 changes: 32 additions & 0 deletions src/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions src/type.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 4b3ac9e

Please sign in to comment.