Skip to content

Commit

Permalink
-autoconfig option added, unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thradams committed Jun 24, 2024
1 parent 01b380f commit 81ff3eb
Show file tree
Hide file tree
Showing 24 changed files with 1,833 additions and 1,144 deletions.
7 changes: 7 additions & 0 deletions manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ Sample of cakeconfig.h

```

Cake also have the option -autoconfig that generates the cakeconfig.h automatically.

Cake also includes standard header files. The objective is to allow usage even without installing GCC or MSVC. You can set this path on `cakeconfig.h` but mixing cake headers with other headers is not recommended.

## Command line
Expand Down Expand Up @@ -140,6 +142,11 @@ Output is compatible with visual studio IDE. We can click on the error message a
This option enables an static analysis of program flow. This is required for some
ownership checks

### -autoconfig
Generates cakeconfig.h header.
On Windows, it must be generated inside the Visual Studio Command Prompt to read the INCLUDE variable.
On Linux, it calls GCC with echo | gcc -v -E - 2>&1 and reads the output.

## Output

One directory called **out** is created keeping the same directory structure of the input files.
Expand Down
11 changes: 6 additions & 5 deletions src/build.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,14 @@ int main()
To find GCC directories use
echo | gcc -E -Wp,-v -
*/
if (mysytem("./cake "

//Generates cakeconfig.h
mysytem("./cake -autoconfig");

//Uses previouly generated cakeconfig.h to find include dir
if (mysytem("./cake "
" -D__x86_64__ "
" -fanalyzer "
" -I/usr/lib/gcc/x86_64-linux-gnu/11/include/ "
" -I/usr/local/include/ "
" -I/usr/include/x86_64-linux-gnu/ "
" -I/usr/include/ "
HEADER_FILES
SOURCE_FILES) != 0)
{
Expand Down
31 changes: 0 additions & 31 deletions src/cakeconfig.h

This file was deleted.

47 changes: 33 additions & 14 deletions src/expressions.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ double constant_value_to_double(const struct constant_value* a)
case TYPE_UNSIGNED_LONG_LONG:
return (double)a->ullvalue;
default:
return 0;
break;
}

return 0;
Expand Down Expand Up @@ -103,7 +103,7 @@ unsigned long long constant_value_to_ull(const struct constant_value* a)
case TYPE_UNSIGNED_LONG_LONG:
return (unsigned long long)a->ullvalue;
default:
return 0;
break;
}

return 0;
Expand All @@ -119,7 +119,7 @@ long long constant_value_to_ll(const struct constant_value* a)
case TYPE_UNSIGNED_LONG_LONG:
return (long long)a->ullvalue;
default:
return 0;
break;
}

return 0;
Expand All @@ -135,7 +135,7 @@ bool constant_value_to_bool(const struct constant_value* a)
case TYPE_UNSIGNED_LONG_LONG:
return a->ullvalue != 0;
default:
return 0;
break;
}

return 0;
Expand Down Expand Up @@ -529,14 +529,14 @@ static int compare_function_arguments(struct parser_ctx* ctx,
p_current_parameter_type = p_param_list->head;
}


struct argument_expression* p_current_argument = p_argument_expression_list->head;

while (p_current_argument && p_current_parameter_type)
{
check_assigment(ctx, &p_current_parameter_type->type, p_current_argument->expression, ASSIGMENT_TYPE_PARAMETER);
p_current_argument = p_current_argument->next;
p_current_parameter_type = p_current_parameter_type->next;
p_current_parameter_type = p_current_parameter_type->next;
}

if (p_current_argument != NULL && !p_param_list->is_var_args)
Expand Down Expand Up @@ -2978,7 +2978,7 @@ struct expression* owner additive_expression(struct parser_ctx* ctx)
new_expression->left = p_expression_node;
p_expression_node = NULL; /*MOVED*/


new_expression->right = multiplicative_expression(ctx);
if (new_expression->right == NULL)
{
Expand Down Expand Up @@ -3305,10 +3305,11 @@ struct expression* owner relational_expression(struct parser_ctx* ctx)
return p_expression_node;
}

static void check_diferent_enuns(struct parser_ctx* ctx,
void check_diferent_enuns(struct parser_ctx* ctx,
const struct token* operator_token,
struct expression* left,
struct expression* right)
struct expression* right,
const char * message)
{
if (left->type.type_specifier_flags & TYPE_SPECIFIER_ENUM &&
right->type.type_specifier_flags & TYPE_SPECIFIER_ENUM)
Expand All @@ -3324,10 +3325,18 @@ static void check_diferent_enuns(struct parser_ctx* ctx,
if (right->type.enum_specifier->tag_token)
righttag = right->type.enum_specifier->tag_token->lexeme;

char finalmessage[200]={0};
snprintf(finalmessage,
sizeof finalmessage,
"%s (enum %s, enum %s)",
message,
lefttag,
righttag);

compiler_diagnostic_message(W_ENUN_CONVERSION,
ctx,
operator_token,
"implicit conversion from 'enum %s' to 'enum %s'",
finalmessage,
lefttag,
righttag);
}
Expand Down Expand Up @@ -3391,7 +3400,8 @@ struct expression* owner equality_expression(struct parser_ctx* ctx)
throw;

new_expression->last_token = new_expression->right->last_token;
check_diferent_enuns(ctx, operator_token, new_expression->left, new_expression->right);
check_diferent_enuns(ctx, operator_token, new_expression->left, new_expression->right,
"comparing different enums.");

new_expression->first_token = operator_token;

Expand Down Expand Up @@ -3557,7 +3567,6 @@ struct expression* owner exclusive_or_expression(struct parser_ctx* ctx)

struct expression* owner inclusive_or_expression(struct parser_ctx* ctx)
{

/*
inclusive-OR-expression:
exclusive-OR-expression
Expand All @@ -3573,6 +3582,7 @@ struct expression* owner inclusive_or_expression(struct parser_ctx* ctx)
while (ctx->current != NULL &&
(ctx->current->type == '|'))
{
struct token* operator_token = ctx->current;
parser_match(ctx);
struct expression* owner new_expression = calloc(1, sizeof * new_expression);
if (new_expression == NULL)
Expand All @@ -3590,6 +3600,13 @@ struct expression* owner inclusive_or_expression(struct parser_ctx* ctx)
expression_delete(new_expression);
throw;
}

check_diferent_enuns(ctx,
operator_token,
new_expression->left,
new_expression->right,
"operator '|' between enumerations of different types.");

new_expression->last_token = new_expression->right->last_token;
new_expression->constant_value =
constant_value_op(&new_expression->left->constant_value, &new_expression->right->constant_value, '|');
Expand Down Expand Up @@ -3845,7 +3862,9 @@ struct expression* owner assignment_expression(struct parser_ctx* ctx)
new_expression->type.storage_class_specifier_flags &= ~STORAGE_SPECIFIER_FUNCTION_RETURN;
new_expression->type.storage_class_specifier_flags &= ~STORAGE_SPECIFIER_FUNCTION_RETURN_NODISCARD;

check_diferent_enuns(ctx, op_token, new_expression->left, new_expression->right);
check_diferent_enuns(ctx, op_token, new_expression->left, new_expression->right,
"assignment of different enums.");

new_expression->left->is_assigment_expression = true;
if (new_expression->left->left)
new_expression->left->left->is_assigment_expression = true;
Expand All @@ -3869,7 +3888,7 @@ void argument_expression_list_push(struct argument_expression_list* list, struct
}
else
{
assert(list->tail->next == NULL);
assert(list->tail->next == NULL);
list->tail->next = pitem;
}
list->tail = pitem;
Expand Down
6 changes: 6 additions & 0 deletions src/expressions.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,9 @@ void expression_evaluate_equal_not_equal(const struct expression* left,
struct expression* result,
int op,
bool disabled);

void check_diferent_enuns(struct parser_ctx* ctx,
const struct token* operator_token,
struct expression* left,
struct expression* right,
const char * message);
24 changes: 8 additions & 16 deletions src/file.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
#pragma safety enable
enum E1 {A, B};
enum E2 {C, D};



void free( void* _Owner ptr);
void* _Owner malloc(int size);
struct X { char * _Owner text; };

void x_destroy(struct X* _Obj_owner p)
void f(enum E1 e)
{
free(p->text);
}

void x_delete(struct X* _Owner _Opt p)
{
if (p)
switch(e)
{
x_destroy(p);
free(p);
case A:break;
case 12:break;
}
}
}
int main(){}
Loading

0 comments on commit 81ff3eb

Please sign in to comment.