Skip to content

Commit

Permalink
_Opt for struct (not ready yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
thradams committed Apr 21, 2024
1 parent 700892e commit 65a9f69
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 13 deletions.
6 changes: 3 additions & 3 deletions ownership.md
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ When the object is created on the stack, we can implement a destructor.
#include <stdlib.h>

struct X {
char * _Owner text;
char * _Owner _Opt _text;
};

void x_destroy(struct X x) {
Expand Down Expand Up @@ -526,10 +526,10 @@ The next sample illustrates how to implement a destructor using a _Obj_owner poi
#include <stdlib.h>
struct X {
char * _Owner text;
char * _Owner _Opt text;
};
void x_destroy(struct X * _Obj_owner x) {
void x_destroy(_Opt _struct X * _Obj_owner x) {
free(x->text);
/*x is not the owner of the memory*/
}
Expand Down
17 changes: 8 additions & 9 deletions src/file.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#pragma nullable enable
#pragma ownership enable

void free(void * _Owner _Opt p);
struct X {
void * _Owner _Opt text;
char * _Owner text;
};
void x_destroy(struct X x);

struct X f();

int main()
{
struct X x = f();
_View struct X x2 = x;

void x_destroy(_Opt struct X * _Obj_owner x) {
free(x->text);
}

int main() {
struct X x = {};
x_destroy(&x);
}
4 changes: 4 additions & 0 deletions src/flow_visit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,7 @@ static void flow_visit_init_declarator_new(struct flow_visit_ctx* ctx, struct in
ASSIGMENT_TYPE_OBJECTS,
false,
type_is_view(&p_init_declarator->p_declarator->type),
type_is_nullable(&p_init_declarator->p_declarator->type, ctx->ctx->options.null_checks_enabled),
&p_init_declarator->p_declarator->type,
&p_init_declarator->p_declarator->object,
&p_init_declarator->initializer->assignment_expression->type,
Expand Down Expand Up @@ -1759,6 +1760,7 @@ static void compare_function_arguments3(struct parser_ctx* ctx,
ASSIGMENT_TYPE_PARAMETER,
true,
type_is_view(&p_current_parameter_type->type),
type_is_nullable(&p_current_parameter_type->type, ctx->options.null_checks_enabled),
&p_current_parameter_type->type,
&parameter_object, /*dest object*/

Expand Down Expand Up @@ -2254,6 +2256,7 @@ static void flow_visit_expression(struct flow_visit_ctx* ctx, struct expression*
ASSIGMENT_TYPE_OBJECTS,
true,
type_is_view(&p_expression->left->type), /*dest type*/
type_is_nullable(&p_expression->left->type, ctx->ctx->options.null_checks_enabled), /*dest type*/
&p_expression->left->type, /*dest type*/
p_dest_object, /*dest object*/
&p_expression->right->type, /*source type*/
Expand Down Expand Up @@ -2671,6 +2674,7 @@ static void flow_visit_jump_statement(struct flow_visit_ctx* ctx, struct jump_st
ASSIGMENT_TYPE_RETURN,
true,
type_is_view(ctx->p_return_type), /*dest type*/
type_is_nullable(ctx->p_return_type, ctx->ctx->options.null_checks_enabled), /*dest type*/
ctx->p_return_type, /*dest type*/
&dest_object, /*dest object*/
&p_jump_statement->expression_opt->type, /*source type*/
Expand Down
2 changes: 2 additions & 0 deletions src/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2831,6 +2831,7 @@ void object_assignment3(
enum assigment_type assigment_type,
bool check_uninitialized_b,
bool a_type_is_view,
bool a_type_is_nullable,
struct type* p_a_type, struct object* p_a_object,
struct type* p_b_type, struct object* p_b_object)
{
Expand Down Expand Up @@ -3216,6 +3217,7 @@ void object_assignment3(
assigment_type,
check_uninitialized_b,
a_type_is_view,
a_type_is_nullable,
p_a_member_type, p_a_member_object,
p_b_member_type, p_b_member_object);
}
Expand Down
1 change: 1 addition & 0 deletions src/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ void object_assignment3(struct parser_ctx* ctx,
enum assigment_type assigment_type,
bool check_uninitialized_b,
bool a_type_is_view,
bool a_type_is_nullable,
struct type* p_a_type, struct object* p_a_object,
struct type* p_b_type, struct object* p_b_object);

Expand Down
1 change: 1 addition & 0 deletions src/type.c
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,7 @@ bool type_is_view(const struct type* p_type)




bool type_is_out(const struct type* p_type)
{
return p_type->type_qualifier_flags & TYPE_QUALIFIER_OUT;
Expand Down
2 changes: 1 addition & 1 deletion src/web/ownership.html
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ <h3 id="toc_5">View references</h3>
#include &lt;stdlib.h&gt;

struct X {
char * _Owner text;
char * _Owner _Opt text;
};

void f(_View struct X x) { /*...*/ }
Expand Down
16 changes: 16 additions & 0 deletions tests/unit-tests/test_17700.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma nullable enable
#pragma ownership enable

void free(void * _Owner _Opt p);
struct X {
char * _Owner text;
};

void x_destroy(_Opt struct X * _Obj_owner x) {
free(x->text);
}

int main() {
struct X x = {};
x_destroy(&x);
}

0 comments on commit 65a9f69

Please sign in to comment.