[cfe-dev] warning request: partial array initialisation

Jay Foad jay.foad at gmail.com
Thu Jun 21 01:53:09 PDT 2012


On 19 June 2012 20:31, Jay Foad <jay.foad at gmail.com> wrote:
> Fair point. I agree with the general principle that the compiler
> shouldn't warn when there's no better way to express what you really
> want; and if I really want
>
>  static int a[1000000] = { 1, 2, /* all the rest 0 */ };
>
> then there's no better way of writing it.
>
>> Also, this seems really like a coding convention or style you would like to enforce, and so I suspect a separate tool would be better suited to it.
>
> Yup. I'd love a general purpose scriptable tool for doing this kind of
> thing. Maybe it's time I looked at Coccinelle...

Just to follow up on this (at the risk of going seriously off-topic) I
managed to get most of what I wanted with a Coccinelle/Python script:

--------
@ r @
type T;
identifier A;
position p;
expression M;
initializer list[N] X;
@@

T A at p[M] = { X };

@ script:python @
A << r.A;
p << r.p;
M << r.M;
N << r.N;
@@

print "%s(%s): %s[%s] = { ... %s items ... }" % (p[0].file, p[0].line, A, M, N)
--------

... and then eyeballing the output for anything that looked suspicious.

The script prints things like:

foo.c(123): myarray[FOO_MAX + 1] = { ... 17 items .. }

It would be even better if it could evaluate the constant expression
FOO_MAX + 1 for me, but I don't know if Coccinelle is capable of that.

Jay.




More information about the cfe-dev mailing list