[Swan-dev] Add -std=c99 to the compiler flags?

Andrew Cagney andrew.cagney at gmail.com
Thu Feb 5 00:00:18 EET 2015


[inline]

On 4 February 2015 at 15:33, D. Hugh Redelmeier <hugh at mimosa.com> wrote:
> | From: Andrew Cagney <andrew.cagney at gmail.com>
>
> | I've a simple motivation for this, I'd like to be able to use:
> |
> |     for (int i = 0; i < 10; i++) { ... }
> |
> | without -std=c99 I get a warning :-)
>
> When the code was originally written, there was no c99.  I guess we
> are long past those days and we should start taking advantage of the
> new features.
>
> I think declaring the variable in the for-head is one of the nicer
> little features.  It goes along with my desire to keep variables as
> local as possible.
>
> There has been a strong temptation to use the same index variable in
> sibling for statements: it cuts down in the number of declarations and
> lines.  I think this new form eliminates that excuse.
>
> Why does gcc require a flag?  Is using c99 so remarkable that it cannot be
> the default?  Is there any downside?  At least some things we already use
> were introduced in C99.

The default for GCC (i.e, up to 4.x) is -std=gnu90 ("GNU dialect of
ISO C90 (including some C99 features)").  That is why we get away with
doing some things, but other things get warnings.

In GCC 5.x the default switches to -std=gnu11.  I suspect the long
painful and drawn out process of trying to implement all the C99
features is why it is being skipped.

> What other C99 features are worth using?
>
> - local declarations mixed in with statements.  We've avoided this
>   (mostly) but it is sometimes quite nice.  Actually, this predates C99.
>   This allows us to have fewer variables that are uninitialized.
>
> - I think that we already use the struct and union member initializing
>   syntax

GNU and C99 had different syntax, I believe we're using the new syntax.

> - Variable Length Arrays (VLAs) have been made optional-to-support in the
>   more recent standards so we should not use them.  Too bad: they could
>   do what alloca was used for.

There's dogma arguing that we, as (what is the buzz phrase) an attack
surface, should never allocate dynamic sized structures on the stack.

> - inline functions -- we already use them.  How come we don't need
>   -std=c99 already?

The behaviour of inline also changed.  Specifically this case:

static __inline__ struct hash_desc *ike_alg_get_hasher(int alg)
{
        return (struct hash_desc *) ikev1_alg_find(IKE_ALG_HASH, alg);
}

(I'm guessing the intent is for this to be a strongly typed macro)

IMNSHO we shouldn't be using inline any way.  We're deluding ourselves
if we think we can out wit the compiler: GCC will inline the things it
likes regardless; and issue warning pointing out how we're wrong :-)

> - <stdbool.h>: we should replace the bool abstraction that I created.
>   Should be trivial
>
> - <inttypes.h> I think we already use it

We should be using it.  Often we don't.

> - // we use this a bit.  I don't like it except in special cases.
>
> - compound literals: might be useful

Andrew


More information about the Swan-dev mailing list