Skip to content

Commit

Permalink
fixes #220
Browse files Browse the repository at this point in the history
  • Loading branch information
thradams committed Dec 28, 2024
1 parent 6c5add5 commit 96ee3f5
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 172 deletions.
14 changes: 8 additions & 6 deletions src/file.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
struct X {
int x;
};
#pragma flow enable

int main() {
constexpr struct X x = (struct X){ .x = 50 };
static_assert(x.x == 50);
int *f();
int main()
{
if (int *p = f())
{
static_state(p, "not-null");
}
}
36 changes: 25 additions & 11 deletions src/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -27967,7 +27967,7 @@ void defer_start_visit_declaration(struct defer_visit_ctx* ctx, struct declarati

//#pragma once

#define CAKE_VERSION "0.9.45"
#define CAKE_VERSION "0.9.46"



Expand Down Expand Up @@ -43680,6 +43680,12 @@ static void flow_visit_if_statement(struct flow_visit_ctx* ctx, struct selection
flow_visit_simple_declaration(ctx, p_selection_statement->p_init_statement->p_simple_declaration);


/*
if(int *p = f());
The hidden expression is p
*/
struct expression hidden_expression = { 0 };

struct true_false_set true_false_set = { 0 };

if (p_selection_statement->condition &&
Expand All @@ -43695,6 +43701,14 @@ static void flow_visit_if_statement(struct flow_visit_ctx* ctx, struct selection
flow_visit_init_declarator(ctx, p_selection_statement->condition->p_init_declarator);
}

if (p_selection_statement->condition &&
p_selection_statement->condition->expression == NULL)
{
hidden_expression.expression_type = PRIMARY_EXPRESSION_DECLARATOR;
hidden_expression.declarator = p_selection_statement->condition->p_init_declarator->p_declarator;
flow_visit_expression(ctx, &hidden_expression, &true_false_set);
}

assert(p_selection_statement->first_token->type == TK_KEYWORD_IF);

/*
Expand Down Expand Up @@ -44214,7 +44228,7 @@ static void check_uninitialized(struct flow_visit_ctx* ctx, struct expression* p
}
}
}
}
}
}

void flow_object_push_states_from(const struct flow_object* p_object_from, struct flow_object* p_object_to)
Expand Down Expand Up @@ -44276,7 +44290,7 @@ static void flow_check_pointer_used_as_bool(struct flow_visit_ctx* ctx, struct e
&marker,
"pointer is always not-null");
}
}
}
}
}

Expand Down Expand Up @@ -44898,7 +44912,7 @@ static void flow_visit_expression(struct flow_visit_ctx* ctx, struct expression*
object_set_pointer(p_dest_object, po);
type_destroy(&t);
p_dest_object->current.state = FLOW_OBJECT_STATE_NOT_NULL | FLOW_OBJECT_STATE_NULL;
}
}
}
break;
case MULTIPLICATIVE_EXPRESSION_DIV:
Expand Down Expand Up @@ -45740,7 +45754,7 @@ static void flow_visit_jump_statement(struct flow_visit_ctx* ctx, struct jump_st
}
else if (p_jump_statement->first_token->type == TK_KEYWORD_BREAK)
{
arena_merge_current_state_with_state_number(ctx, ctx->break_join_state);
arena_merge_current_state_with_state_number(ctx, ctx->break_join_state);
flow_exit_block_visit_defer_list(ctx, &p_jump_statement->defer_list, p_jump_statement->first_token);
}
else if (p_jump_statement->first_token->type == TK_KEYWORD_GOTO)
Expand Down Expand Up @@ -46102,7 +46116,7 @@ static void flow_visit_direct_declarator(struct flow_visit_ctx* ctx, struct dire

while (parameter)
{

flow_visit_declaration_specifiers(ctx, parameter->declaration_specifiers, &parameter->declarator->type);

if (parameter->declarator)
Expand Down Expand Up @@ -46346,7 +46360,7 @@ static void flow_visit_type_specifier_qualifier(struct flow_visit_ctx* ctx, stru
if (p_type_specifier_qualifier->type_specifier)
{
flow_visit_type_specifier(ctx, p_type_specifier_qualifier->type_specifier);
}
}
}

static void flow_visit_declaration_specifier(struct flow_visit_ctx* ctx, struct declaration_specifier* p_declaration_specifier)
Expand Down Expand Up @@ -46417,7 +46431,7 @@ void flow_visit_declaration(struct flow_visit_ctx* ctx, struct declaration* p_de
{
flow_visit_pragma_declaration(ctx, p_declaration->pragma_declaration);
}


if (p_declaration->declaration_specifiers)
{
Expand All @@ -46444,7 +46458,7 @@ void flow_visit_declaration(struct flow_visit_ctx* ctx, struct declaration* p_de

struct type type = get_function_return_type(&p_declaration->init_declarator_list.head->p_declarator->type);
ctx->p_return_type = &type;

flow_visit_compound_statement(ctx, p_declaration->function_body);
type_destroy(&type);
ctx->p_return_type = NULL;
Expand All @@ -46464,9 +46478,9 @@ void flow_start_visit_declaration(struct flow_visit_ctx* ctx, struct declaration
assert(p_declaration->function_body != NULL); //flow_visit_declaration does not change this

if (!flow_is_last_item_return(p_declaration->function_body))
{
{
flow_exit_block_visit_defer_list(ctx, &p_declaration->defer_list, p_declaration->function_body->last_token);
}
}
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

#pragma once

#define CAKE_VERSION "0.9.45"
#define CAKE_VERSION "0.9.46"


34 changes: 24 additions & 10 deletions src/visit_flow.c
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,12 @@ static void flow_visit_if_statement(struct flow_visit_ctx* ctx, struct selection
flow_visit_simple_declaration(ctx, p_selection_statement->p_init_statement->p_simple_declaration);


/*
if(int *p = f());
The hidden expression is p
*/
struct expression hidden_expression = { 0 };

struct true_false_set true_false_set = { 0 };

if (p_selection_statement->condition &&
Expand All @@ -929,6 +935,14 @@ static void flow_visit_if_statement(struct flow_visit_ctx* ctx, struct selection
flow_visit_init_declarator(ctx, p_selection_statement->condition->p_init_declarator);
}

if (p_selection_statement->condition &&
p_selection_statement->condition->expression == NULL)
{
hidden_expression.expression_type = PRIMARY_EXPRESSION_DECLARATOR;
hidden_expression.declarator = p_selection_statement->condition->p_init_declarator->p_declarator;
flow_visit_expression(ctx, &hidden_expression, &true_false_set);
}

assert(p_selection_statement->first_token->type == TK_KEYWORD_IF);

/*
Expand Down Expand Up @@ -1448,7 +1462,7 @@ static void check_uninitialized(struct flow_visit_ctx* ctx, struct expression* p
}
}
}
}
}
}

void flow_object_push_states_from(const struct flow_object* p_object_from, struct flow_object* p_object_to)
Expand Down Expand Up @@ -1510,7 +1524,7 @@ static void flow_check_pointer_used_as_bool(struct flow_visit_ctx* ctx, struct e
&marker,
"pointer is always not-null");
}
}
}
}
}

Expand Down Expand Up @@ -2132,7 +2146,7 @@ static void flow_visit_expression(struct flow_visit_ctx* ctx, struct expression*
object_set_pointer(p_dest_object, po);
type_destroy(&t);
p_dest_object->current.state = FLOW_OBJECT_STATE_NOT_NULL | FLOW_OBJECT_STATE_NULL;
}
}
}
break;
case MULTIPLICATIVE_EXPRESSION_DIV:
Expand Down Expand Up @@ -2974,7 +2988,7 @@ static void flow_visit_jump_statement(struct flow_visit_ctx* ctx, struct jump_st
}
else if (p_jump_statement->first_token->type == TK_KEYWORD_BREAK)
{
arena_merge_current_state_with_state_number(ctx, ctx->break_join_state);
arena_merge_current_state_with_state_number(ctx, ctx->break_join_state);
flow_exit_block_visit_defer_list(ctx, &p_jump_statement->defer_list, p_jump_statement->first_token);
}
else if (p_jump_statement->first_token->type == TK_KEYWORD_GOTO)
Expand Down Expand Up @@ -3336,7 +3350,7 @@ static void flow_visit_direct_declarator(struct flow_visit_ctx* ctx, struct dire

while (parameter)
{

flow_visit_declaration_specifiers(ctx, parameter->declaration_specifiers, &parameter->declarator->type);

if (parameter->declarator)
Expand Down Expand Up @@ -3580,7 +3594,7 @@ static void flow_visit_type_specifier_qualifier(struct flow_visit_ctx* ctx, stru
if (p_type_specifier_qualifier->type_specifier)
{
flow_visit_type_specifier(ctx, p_type_specifier_qualifier->type_specifier);
}
}
}

static void flow_visit_declaration_specifier(struct flow_visit_ctx* ctx, struct declaration_specifier* p_declaration_specifier)
Expand Down Expand Up @@ -3651,7 +3665,7 @@ void flow_visit_declaration(struct flow_visit_ctx* ctx, struct declaration* p_de
{
flow_visit_pragma_declaration(ctx, p_declaration->pragma_declaration);
}


if (p_declaration->declaration_specifiers)
{
Expand All @@ -3678,7 +3692,7 @@ void flow_visit_declaration(struct flow_visit_ctx* ctx, struct declaration* p_de

struct type type = get_function_return_type(&p_declaration->init_declarator_list.head->p_declarator->type);
ctx->p_return_type = &type;

flow_visit_compound_statement(ctx, p_declaration->function_body);
type_destroy(&type);
ctx->p_return_type = NULL;
Expand All @@ -3698,9 +3712,9 @@ void flow_start_visit_declaration(struct flow_visit_ctx* ctx, struct declaration
assert(p_declaration->function_body != NULL); //flow_visit_declaration does not change this

if (!flow_is_last_item_return(p_declaration->function_body))
{
{
flow_exit_block_visit_defer_list(ctx, &p_declaration->defer_list, p_declaration->function_body->last_token);
}
}
}
else
{
Expand Down
300 changes: 156 additions & 144 deletions src/web/cake.js

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions tests/unit-tests/parser_if_with_initializer_flow.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma flow enable

int *f();
int main()
{
if (int *p = f())
{
static_state(p, "not-null");
}
}

0 comments on commit 96ee3f5

Please sign in to comment.