Email List: Xaustin-group-lX
[All Lists]

Re: c99 -I option and GCC

To: austin-group-l@xxxxxxxxxxxxx
Subject: Re: c99 -I option and GCC
From: Geoff Clare <gwc@xxxxxxxxxxxxx>
Date: Wed, 5 Aug 2009 15:59:11 +0100
References: <4a76fb43.lbC4QjMxLz0GOnAz%Joerg.Schilling@fokus.fraunhofer.de> <200908031526.n73FQ0M7002876@aragorn.savarese.org> <20090804003819.GE3746@prunille.vinc17.org> <20090804083311.GA29260@squonk.masqnet> <20090804115624.GD1239@prunille.vinc17.org>
Vincent Lefevre <vincent-opgr@vinc17.org> wrote, on 04 Aug 2009:
>
> On 2009-08-04 09:33:11 +0100, Geoff Clare wrote:
> > Vincent Lefevre <vincent-opgr@vinc17.org> wrote, on 04 Aug 2009:
> > > What's the behavior if one gives several -D options with the same name
> > > and different values (and no corresponding -U option)?
> > 
> > A diagnostic message must be written.  This is because the description
> > of -D says it defines the named macro "as if by a C-language #define
> > directive", and C99 requires a diagnostic if there are multiple
> > #define directives for the same name with different values (or more
> > specifically, where the preprocessing tokens in the values do not
> > have "the same number, ordering, spelling, and white-space separation,
> > where all white-space separations are considered identical").
> 
> and the behavior is undefined (if code is generated), I suppose.
> 
> But this is not obvious, as sometimes options override other options.

The default behaviour when an option that has option-arguments is
repeated is stated in Syntax Guideline 11:

    If an option that has option-arguments is repeated, the option and
    option-argument combinations should [shall for standard utilities]
    be interpreted in the order specified on the command line.

In cases where one overrides the other, it would need to be stated
explicitly in the utility description.

> And how about the following?
> 
>   c99 tst.c -UFOO -DFOO=1 -DFOO=2
>   c99 tst.c -DFOO=1 -UFOO -DFOO=2
>   c99 tst.c -DFOO=1 -DFOO=2 -UFOO
> 
> Should this be accepted or not? POSIX says that -D has a lower
> precedence. Does this mean that the -D options are ignored here
> or that one has something equivalent to the following code?
> 
> #define FOO 1
> #define FOO 2
> #undef FOO

The standard says that when a -D and a -U are specified for the
same name, the -U overrides the -D (regardless of order).  However,
it is silent about the case of multiple -D's and a -U, so an
implementation could either ignore all of the -D options in
those commands, or it could write a diagnostic message when it
processes the second -D even though (if it continues with code
generation) the value(s) are not going to be used.

> Also, what if one has a single -D but the name is predefined by the
> compiler and the value is different? I suppose that one should have
> a diagnostic too (and undefined behavior).

Yes, a diagnostic is required (just the same as if the source had
a #define of that macro name).

> And what if there's a newline character in the value? The GCC man
> page documents: "the definition will be truncated by embedded newline
> characters".

My reading is that

    c99 -Dname='one\
    two'

should work the same as if the source contained

    #define name one\
    two

but a newline not preceded by a backslash would be undefined
behaviour (since the "as-if by #define" requirement can't be
satisfied in that case).  However, I doubt if this case was
considered when the description of c89 -D was written in
POSIX.2, so the behaviour (with backslash) may well not match
historical practise.

-- 
Geoff Clare <g.clare@opengroup.org>
The Open Group, Thames Tower, Station Road, Reading, RG1 1LX, England

<Prev in Thread] Current Thread [Next in Thread>