{ .field = value } struct initialization alternative

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

{ .field = value } struct initialization alternative

Ivan Medoedov
Hello,

{ .field = value } syntax is not supported by TCC.

What's the best alternative? Or is it simply not possible and I should use

foo.field = value; foo.field2=value; etc

Thanks

_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel
Reply | Threaded
Open this post in threaded view
|

Re: { .field = value } struct initialization alternative

Michael Matz-4
Hi,

On Sun, 21 Jul 2019, Ivan Medoedov wrote:

> Hello,
> { .field = value } syntax is not supported by TCC.

Why do you think so?

% cat designated-init.c
struct S {
     int a, b;
};

struct S s = {.a = 2, .b = 3};
% ./tcc -c designated-init.c && echo ok
ok
%


Ciao,
Michael.

_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel
Reply | Threaded
Open this post in threaded view
|

Re: { .field = value } struct initialization alternative

Ivan Medoedov
Indeed. Sorry, my bad.

Looks like the error is caused by __VA_ARGS__

I'm using the following macro: 

#define ALLOC_INIT(type, ...) (type *)memdup((type[]){ __VA_ARGS__ }, sizeof(type))


Foo* foo = ALLOC_INIT(Foo, { field: 123, field2: 456 });

error: field expected

This macro works fine with GCC and Clang.


On Sun, Jul 21, 2019 at 6:50 PM Michael Matz <[hidden email]> wrote:
Hi,

On Sun, 21 Jul 2019, Ivan Medoedov wrote:

> Hello,
> { .field = value } syntax is not supported by TCC.

Why do you think so?

% cat designated-init.c
struct S {
     int a, b;
};

struct S s = {.a = 2, .b = 3};
% ./tcc -c designated-init.c && echo ok
ok
%


Ciao,
Michael.

_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel
Reply | Threaded
Open this post in threaded view
|

Re: { .field = value } struct initialization alternative

Ivan Medoedov
Typo:

Foo* foo = ALLOC_INIT(Foo, { .field = 123, .field2 = 456 });


On Sun, Jul 21, 2019 at 7:56 PM Ivan Medoedov <[hidden email]> wrote:
Indeed. Sorry, my bad.

Looks like the error is caused by __VA_ARGS__

I'm using the following macro: 

#define ALLOC_INIT(type, ...) (type *)memdup((type[]){ __VA_ARGS__ }, sizeof(type))


Foo* foo = ALLOC_INIT(Foo, { field: 123, field2: 456 });

error: field expected

This macro works fine with GCC and Clang.


On Sun, Jul 21, 2019 at 6:50 PM Michael Matz <[hidden email]> wrote:
Hi,

On Sun, 21 Jul 2019, Ivan Medoedov wrote:

> Hello,
> { .field = value } syntax is not supported by TCC.

Why do you think so?

% cat designated-init.c
struct S {
     int a, b;
};

struct S s = {.a = 2, .b = 3};
% ./tcc -c designated-init.c && echo ok
ok
%


Ciao,
Michael.

_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel
Reply | Threaded
Open this post in threaded view
|

Re: { .field = value } struct initialization alternative

Michael Matz-4
Hi,

On Sun, 21 Jul 2019, Ivan Medoedov wrote:

> Typo:
>
> Foo* foo = ALLOC_INIT(Foo, { .field = 123, .field2 = 456 });

Next time, please be specific in your bug reports, it should contain a
self-contained example of the problem you're seeing.  With the info
available I constructed the example below, and it works just fine.

Then I realized that maybe you indeed mean GNU designated initializers and
the runtime behaviour instead of not being able to compile something.  I
was able to generate a segfault at runtime when using the GNU form (i.e.
'{ foo: val }').  That's now fixed in mob.

% cat alloc-init.c
typedef __SIZE_TYPE__ size_t;
struct S {
     int a, b;
};

extern void * malloc(size_t);
extern void * memcpy(void *, const void *, size_t);
extern int printf(const char *, ...);
void *memdup(const void *src, size_t sz);
#define ALLOC_INIT(type, ...)   \
         (type *)memdup((type[]){ __VA_ARGS__ }, sizeof(type))

int main()
{
   struct S *ps = ALLOC_INIT(struct S, {.a = 3, .b = 4});
   printf("%d %d\n", ps->a, ps->b);
   return 0;
}

void *memdup(const void *src, size_t sz) {
         void *mem = malloc(sz);
         return mem ? memcpy(mem, src, sz) : 0;
}

Ciao,
Michael.

>
>
> On Sun, Jul 21, 2019 at 7:56 PM Ivan Medoedov <[hidden email]>
> wrote:
>       Indeed. Sorry, my bad.
> Looks like the error is caused by __VA_ARGS__
>
> I'm using the following macro: 
>
> #define ALLOC_INIT(type, ...) (type *)memdup((type[]){ __VA_ARGS__ },
> sizeof(type))
>
> https://tia.mat.br/posts/2015/05/01/initializing_a_heap_allocated_structure
> _in_c.html
>
> Foo* foo = ALLOC_INIT(Foo, { field: 123, field2: 456 });
>
> error: field expected
>
> This macro works fine with GCC and Clang.
>
>
> On Sun, Jul 21, 2019 at 6:50 PM Michael Matz <[hidden email]>
> wrote:
>       Hi,
>
>       On Sun, 21 Jul 2019, Ivan Medoedov wrote:
>
>       > Hello,
>       > { .field = value } syntax is not supported by TCC.
>
>       Why do you think so?
>
>       % cat designated-init.c
>       struct S {
>            int a, b;
>       };
>
>       struct S s = {.a = 2, .b = 3};
>       % ./tcc -c designated-init.c && echo ok
>       ok
>       %
>
>
>       Ciao,
>       Michael.
>
>       _______________________________________________
>       Tinycc-devel mailing list
>       [hidden email]
>       https://lists.nongnu.org/mailman/listinfo/tinycc-devel
>
>
>
_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel
Reply | Threaded
Open this post in threaded view
|

Re: { .field = value } struct initialization alternative

Ivan Medoedov
No, it's a compilation error (error: field expected), and I can't reproduce it in a small program. The original file has hundreds of thousands of loc.

I'll investigate and send you a small self-contained example.

On Sun, Jul 21, 2019 at 9:20 PM Michael Matz <[hidden email]> wrote:
Hi,

On Sun, 21 Jul 2019, Ivan Medoedov wrote:

> Typo:
>
> Foo* foo = ALLOC_INIT(Foo, { .field = 123, .field2 = 456 });

Next time, please be specific in your bug reports, it should contain a
self-contained example of the problem you're seeing.  With the info
available I constructed the example below, and it works just fine.

Then I realized that maybe you indeed mean GNU designated initializers and
the runtime behaviour instead of not being able to compile something.  I
was able to generate a segfault at runtime when using the GNU form (i.e.
'{ foo: val }').  That's now fixed in mob.

% cat alloc-init.c
typedef __SIZE_TYPE__ size_t;
struct S {
     int a, b;
};

extern void * malloc(size_t);
extern void * memcpy(void *, const void *, size_t);
extern int printf(const char *, ...);
void *memdup(const void *src, size_t sz);
#define ALLOC_INIT(type, ...)   \
         (type *)memdup((type[]){ __VA_ARGS__ }, sizeof(type))

int main()
{
   struct S *ps = ALLOC_INIT(struct S, {.a = 3, .b = 4});
   printf("%d %d\n", ps->a, ps->b);
   return 0;
}

void *memdup(const void *src, size_t sz) {
         void *mem = malloc(sz);
         return mem ? memcpy(mem, src, sz) : 0;
}

Ciao,
Michael.

>
>
> On Sun, Jul 21, 2019 at 7:56 PM Ivan Medoedov <[hidden email]>
> wrote:
>       Indeed. Sorry, my bad.
> Looks like the error is caused by __VA_ARGS__
>
> I'm using the following macro: 
>
> #define ALLOC_INIT(type, ...) (type *)memdup((type[]){ __VA_ARGS__ },
> sizeof(type))
>
> https://tia.mat.br/posts/2015/05/01/initializing_a_heap_allocated_structure
> _in_c.html
>
> Foo* foo = ALLOC_INIT(Foo, { field: 123, field2: 456 });
>
> error: field expected
>
> This macro works fine with GCC and Clang.
>
>
> On Sun, Jul 21, 2019 at 6:50 PM Michael Matz <[hidden email]>
> wrote:
>       Hi,
>
>       On Sun, 21 Jul 2019, Ivan Medoedov wrote:
>
>       > Hello,
>       > { .field = value } syntax is not supported by TCC.
>
>       Why do you think so?
>
>       % cat designated-init.c
>       struct S {
>            int a, b;
>       };
>
>       struct S s = {.a = 2, .b = 3};
>       % ./tcc -c designated-init.c && echo ok
>       ok
>       %
>
>
>       Ciao,
>       Michael.
>
>       _______________________________________________
>       Tinycc-devel mailing list
>       [hidden email]
>       https://lists.nongnu.org/mailman/listinfo/tinycc-devel
>
>
>_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel
Reply | Threaded
Open this post in threaded view
|

Re: { .field = value } struct initialization alternative

Pascal Cuoq
Hello,

On 21 Jul 2019, at 21:37, Ivan Medoedov <[hidden email]> wrote:

No, it's a compilation error (error: field expected), and I can't reproduce it in a small program. The original file has hundreds of thousands of loc.

Large inputs that cause unwarranted compilation errors are easy to make small by using C-Reduce: https://embed.cs.utah.edu/creduce/ (probably already a package of your reputable Linux distribution).

C-Reduce takes as argument a script that indicates that a variant of the initial input is still interesting. In your case, the input is interesting because it is accepted by (say) “gcc -std=c11 -pedantic”  and when compiled with “tcc”, causes stderr to contain “error: field expected”.

It takes a bit of getting used to to wield C-Reduce efficiently; for instance not specifying “-std=c11 -pedantic” or not grepping specifically for “error: field expected” is likely to yield a different bug than the one that was initially bothering you, that C-Reduce will introduce as it reduces the input. John Regehr compares it to “every story where a genie is in a position to grant a wish”.

Even with the quirks that manifest themselves the first couple of times you use it, C-Reduce still helps getting bugs fixed faster than reducing  inputs by hand or not reporting bugs properly because reducing them looks daunting.

Pascal


_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel
Reply | Threaded
Open this post in threaded view
|

Re: { .field = value } struct initialization alternative

Ivan Medoedov
Thanks, I'll try that.

On Sun, Jul 21, 2019 at 9:55 PM Pascal Cuoq <[hidden email]> wrote:
Hello,

On 21 Jul 2019, at 21:37, Ivan Medoedov <[hidden email]> wrote:

No, it's a compilation error (error: field expected), and I can't reproduce it in a small program. The original file has hundreds of thousands of loc.

Large inputs that cause unwarranted compilation errors are easy to make small by using C-Reduce: https://embed.cs.utah.edu/creduce/ (probably already a package of your reputable Linux distribution).

C-Reduce takes as argument a script that indicates that a variant of the initial input is still interesting. In your case, the input is interesting because it is accepted by (say) “gcc -std=c11 -pedantic”  and when compiled with “tcc”, causes stderr to contain “error: field expected”.

It takes a bit of getting used to to wield C-Reduce efficiently; for instance not specifying “-std=c11 -pedantic” or not grepping specifically for “error: field expected” is likely to yield a different bug than the one that was initially bothering you, that C-Reduce will introduce as it reduces the input. John Regehr compares it to “every story where a genie is in a position to grant a wish”.

Even with the quirks that manifest themselves the first couple of times you use it, C-Reduce still helps getting bugs fixed faster than reducing  inputs by hand or not reporting bugs properly because reducing them looks daunting.

Pascal

_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel