Skip to content

Commit 798d4ba

Browse files
committed
nullable pointers, warning removal
1 parent c6add2f commit 798d4ba

File tree

17 files changed

+2517
-2653
lines changed

17 files changed

+2517
-2653
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Cake is a compiler front end written from scratch in C, designed from the C23 la
2121
It allows you to translate newer versions of C, such as C23, to C99. Additionally, Cake provides a platform
2222
for experimenting with new features for the C language, including extensions like lambdas, defer and static [object lifetime](ownership.html) checks.
2323

24+
Cake also has a style checker (no idea why other compilers don't have it) and code formatter.
2425

2526
# Web Playground
2627

src/expressions.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4764,11 +4764,11 @@ void check_assigment(struct parser_ctx* ctx,
47644764
}
47654765
else
47664766
{
4767-
//Everthing else is unusual
4767+
//Everything else is unusual
47684768
// p = false;
47694769
// p = 1-1;
47704770
// p = '\0';
4771-
compiler_diagnostic_message(W_UNSUAL_NULL_POINTER_CONSTANT, ctx, p_b_expression->first_token, NULL, "unusual type used as null pointer constant");
4771+
compiler_diagnostic_message(W_UNSUAL_NULL_POINTER_CONSTANT, ctx, p_b_expression->first_token, NULL, "unusual expression/type used as null pointer constant");
47724772
}
47734773
}
47744774
else

src/file.c

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,6 @@
1010

1111
#pragma safety enable
1212

13-
#include <stdlib.h>
14-
#include <string.h>
13+
#define NULL ((void*)0)
1514

16-
struct nlist { /* table entry: */
17-
struct nlist *next; /* next entry in chain */
18-
char *name; /* defined name */
19-
char *defn; /* replacement text */
20-
};
21-
22-
struct nlist *lookup(char *s);
23-
24-
/* hash: form hash value for string s */
25-
unsigned hash(char *s);
26-
27-
28-
#define HASHSIZE 101
29-
30-
static struct nlist *hashtab[HASHSIZE]; /* pointer table */
31-
32-
/*1* lookup: look for s in hashtab */
33-
struct nlist *lookup(char *);
34-
35-
36-
/* install: put (name, defn) in hashtab */
37-
struct nlist *install(char *name, char *defn)
38-
{
39-
struct nlist *np;
40-
unsigned hashval;
41-
42-
if ((np = lookup(name)) == NULL) { /* not found */
43-
np = (struct nlist *) malloc(sizeof(*np));
44-
if (np == NULL || (np->name = strdup(name)) == NULL)
45-
return NULL;
46-
hashval = hash(name);
47-
np->next = hashtab[hashval];
48-
hashtab[hashval] = np;
49-
} else /* already there */
50-
free((void *) np->defn); /* free previous defn */
51-
52-
if ((np->defn = strdup(defn)) == NULL)
53-
return NULL;
54-
return np;
55-
}
15+
int i = as || NULL;

src/flow_object.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2067,6 +2067,7 @@ void checked_read_object_core(struct flow_visit_ctx* ctx,
20672067
struct object_visitor* p_visitor,
20682068
bool is_nullable,
20692069
const struct token* position_token,
2070+
const struct marker* p_marker,
20702071
bool check_pointed_object,
20712072
const char* previous_names,
20722073
unsigned int visit_number)
@@ -2124,6 +2125,7 @@ void checked_read_object_core(struct flow_visit_ctx* ctx,
21242125
&visitor,
21252126
is_nullable,
21262127
position_token,
2128+
p_marker,
21272129
check_pointed_object,
21282130
buffer,
21292131
visit_number);
@@ -2156,6 +2158,7 @@ void checked_read_object_core(struct flow_visit_ctx* ctx,
21562158
p_visitor,
21572159
is_nullable,
21582160
position_token,
2161+
p_marker,
21592162
check_pointed_object,
21602163
buffer,
21612164
visit_number);
@@ -2179,7 +2182,8 @@ void checked_read_object_core(struct flow_visit_ctx* ctx,
21792182
{
21802183
compiler_diagnostic_message(W_FLOW_NULL_DEREFERENCE,
21812184
ctx->ctx,
2182-
position_token, NULL,
2185+
NULL,
2186+
p_marker,
21832187
"non-nullable pointer '%s' may be null",
21842188
previous_names);
21852189
}
@@ -2200,6 +2204,7 @@ void checked_read_object_core(struct flow_visit_ctx* ctx,
22002204
&visitor,
22012205
is_nullable,
22022206
position_token,
2207+
p_marker,
22032208
true,
22042209
previous_names,
22052210
visit_number);
@@ -2243,6 +2248,7 @@ void checked_read_object(struct flow_visit_ctx* ctx,
22432248
bool is_nullable,
22442249
struct flow_object* p_object,
22452250
const struct token* position_token,
2251+
const struct marker* p_marker,
22462252
bool check_pointed_object)
22472253
{
22482254
const char* _Owner _Opt s = NULL;
@@ -2258,6 +2264,7 @@ void checked_read_object(struct flow_visit_ctx* ctx,
22582264
&visitor,
22592265
is_nullable,
22602266
position_token,
2267+
p_marker,
22612268
check_pointed_object,
22622269
name,
22632270
s_visit_number++);
@@ -2543,22 +2550,25 @@ static void flow_assignment_core(
25432550
{
25442551
compiler_diagnostic_message(W_FLOW_UNINITIALIZED,
25452552
ctx->ctx,
2546-
error_position, NULL,
2553+
NULL,
2554+
p_b_marker,
25472555
"passing an uninitialized argument '%s' object", buffer);
25482556
}
25492557
}
25502558
else if (assigment_type == ASSIGMENT_TYPE_RETURN)
25512559
{
25522560
compiler_diagnostic_message(W_FLOW_UNINITIALIZED,
25532561
ctx->ctx,
2554-
error_position, NULL,
2562+
NULL,
2563+
p_b_marker,
25552564
"returning an uninitialized '%s' object", buffer);
25562565
}
25572566
else
25582567
{
25592568
compiler_diagnostic_message(W_FLOW_UNINITIALIZED,
25602569
ctx->ctx,
2561-
error_position, NULL,
2570+
NULL,
2571+
p_b_marker,
25622572
"reading an uninitialized '%s' object", buffer);
25632573
}
25642574

@@ -2573,7 +2583,8 @@ static void flow_assignment_core(
25732583

25742584
compiler_diagnostic_message(W_FLOW_LIFETIME_ENDED,
25752585
ctx->ctx,
2576-
error_position, NULL,
2586+
NULL,
2587+
p_a_marker,
25772588
"The object '%s' may have been deleted or its lifetime have ended.", buffer);
25782589

25792590

@@ -2690,8 +2701,8 @@ static void flow_assignment_core(
26902701
//if the anwser is yes then we need a warning
26912702
compiler_diagnostic_message(W_FLOW_MISSING_DTOR,
26922703
ctx->ctx,
2693-
error_position,
26942704
NULL,
2705+
p_a_marker,
26952706
"pointed object may be not empty");
26962707
}
26972708
}
@@ -2740,7 +2751,14 @@ static void flow_assignment_core(
27402751
*/
27412752
const bool checked_pointed_object_read = !type_is_out(&t);
27422753
bool is_nullable = a_type_is_nullable || type_is_nullable(&t, ctx->ctx->options.null_checks_enabled);
2743-
checked_read_object(ctx, p_visitor_b->p_type, is_nullable, p_visitor_b->p_object, error_position, checked_pointed_object_read);
2754+
2755+
checked_read_object(ctx,
2756+
p_visitor_b->p_type,
2757+
is_nullable,
2758+
p_visitor_b->p_object,
2759+
error_position,
2760+
p_b_marker,
2761+
checked_pointed_object_read);
27442762

27452763

27462764
//object_copy_state(p_a_type, p_a_object, p_b_type, p_b_object);

src/flow_object.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ enum object_state
4545

4646

4747
struct objects {
48-
struct flow_object* _Owner* _Owner data;
48+
struct flow_object* _Owner* _Owner _Opt data;
4949
int size;
5050
int capacity;
5151
};
@@ -193,6 +193,7 @@ void checked_read_object(struct flow_visit_ctx* ctx,
193193
bool is_nullable,
194194
struct flow_object* p_object,
195195
const struct token* position_token,
196+
const struct marker* p_marker,
196197
bool check_pointed_object);
197198

198199

src/flow_visit.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,11 +1646,16 @@ static void compare_function_arguments3(struct flow_visit_ctx* ctx,
16461646

16471647
if (p_argument_object)
16481648
{
1649+
struct marker marker = {
1650+
.p_token_begin = p_current_argument->expression->first_token,
1651+
.p_token_end = p_current_argument->expression->last_token
1652+
};
16491653
checked_read_object(ctx,
16501654
&p_current_argument->expression->type,
16511655
type_is_nullable(&p_current_argument->expression->type, ctx->ctx->options.null_checks_enabled),
16521656
p_argument_object,
16531657
p_current_argument->expression->first_token,
1658+
&marker,
16541659
false);
16551660
}
16561661
else

0 commit comments

Comments
 (0)