Cherry-picking C99 features (was: [PATCH] xx)

Stefan Metzmacher metze at
Wed Apr 27 22:53:35 UTC 2016

Hi Ralph,

> I just came across an issue caused by our build system not forcing a
> specific C standard, instead relying on cherry-picking C99 features.
> One C99 feature we actively use in our code base and check for at
> configure time are C99 designated initializers.
> But what about other C99 features? What about C99 intermingled code
> and variable declarations, eg declaring the loop variable in a for
> loop:
>   for (int i = 0; ...something...; i++) { ; }
> This builds just fine in a Samba build with newer GCC that have a
> default C standard of at least gnu99, but the build then breaks with
> older gcc that have a default C standard of gnu90. And the poor
> developer with the newer gcc won't notice.
> gnu90 includes C99 designated initializers (yes, really), but not C99
> intermingled code and variable declarations:
>     $ cat test.c
>     struct foo {int x; char y;};
>     struct foo bar = { .y = 'X', .x = 1 };
>     int baz(void) {
>         for (int i=0; i < 2; i++) {
>             bar.x += i;
>         }
>         return bar.x;
>     }
>     $ gcc -std=gnu90 -c test.c
>     test.c: In function ‘baz’:
>     test.c:5:2: error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode
>       for (int i=0; i < 2; i++) {
>       ^
>     test.c:5:2: note: use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code
> What can we do?
> If we decide we *don't* want to support specific C99 features like
> mixed declaration and code, there's just no way to say to the
> compiler "I want C99 feaure A but I don't want C99 feature B, please
> warn me if I inadvertently use it".

We're using -Werror=declaration-after-statement which seems to do the job
to prevent such patches to pass autobuild.

> So I guess cherry-picking is the wrong approach. It should be all or
> nothing.
> Attached is a patch that would continue cherry-picking C99 features
> and adds a check for mixed declarations and code, but this is just
> meant to illustrate the problem.
> Maybe we should take this further and be as pedantic as autoconf's
> # If the C compiler is not in ISO C99 mode by default, try to add an
> # option to output variable CC to make it so.  This macro tries
> # various options that select ISO C99 on some system or another.  It
> # considers the compiler to be in ISO C99 mode if it handles _Bool,
> # // comments, flexible array members, inline, long long int, mixed
> # code and declarations, named initialization of structs, restrict,
> # va_copy, varargs macros, variable declarations in for loops and
> # variable length arrays.

I'd really try to avoid // comments, flexible array members,
mixed code and declarations and variable declarations in for loops.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <>

More information about the samba-technical mailing list