larmel / lacc

A simple, self-hosting C compiler
MIT License
872 stars 64 forks source link

Modified for typedef of imcomplete array. #22

Closed Kray-G closed 4 years ago

Kray-G commented 4 years ago

Hello, larmel.

Thank you for your very good compiler. Now I am using your compiler inside my product because I think it is the best choice.

If you have an interest in it, please see kcci-platform.

It is wonderful for me, but unfortunately I have used the old code because my project was started a year ago. Now I can't use the latest code... sorry but I will be going to maintain it.

Anyway, I would like to feedback something for you from what I have faced in my development with your product.

Here is the problem code. ex) test/typedef-incomplete-array.c.

#include <stdio.h>

static void test_typedef()
{
    typedef int A[];
    A a = { 1, 2 };
    A b = { 3, 4, 5 };
    printf("exp:%d, val:%d\n", 2, sizeof(a) / sizeof(*a));
    printf("exp:%d, val:%d\n" ,3, sizeof(b) / sizeof(*b));
}

int main(void)
{
    test_typedef();
    return 0;
}

I got the following message.

(test/typedef-incomplete-array.c, 7) error: Expected } but got 5.

C89/C90/C99 standard says:

One form of initialization that completes array types involves typedef names. Given the declaration

typedef int A[];        // OK - declared with block scope

the declaration

A a = { 1, 2 }, b = { 3, 4, 5 };

is identical to

int a[] = { 1, 2 }, b[] = { 3, 4, 5 };

due to the rules for incomplete types.

Therefore the result should be:

exp:2, val:2
exp:3, val:3
larmel commented 4 years ago

Hi,

Thank you for reporting, and for a very clear description of the problem.

I spent some time looking at your solution, but was distracted by some issues compiling the latest git code. In the end, I ended up implementing it myself a bit differently, with https://github.com/larmel/lacc/commit/97ed9d095e0396789ee87dec2b5da6b05dc02769.

Very cool to see lacc being used, glad that you find it useful :)