[Python-Dev] if/else and macros (was: CVS: python/dist/src/Objects unicodeobject.c,2.48,2.49)

Greg Stein gstein@lyra.org
Mon, 17 Jul 2000 02:05:32 -0700


On Mon, Jul 17, 2000 at 09:55:02AM +0200, M.-A. Lemburg wrote:
> Greg Stein wrote:
>...
> > Nope. It *does* work. Note the "else" on the end there. That creates the
> > proper binding.
> 
> Greg, I did see the "else", but that does two things: it breaks
> the code if there's no semicolon after the UT8F_ERROR() macro

The current *and* the previous macro would break if there was no semicolon.
No difference here.

> and it produces warnings which relate to the fact that the "else"
> part is ambiguous:
> 
> if (condition) UTF8_ERROR();
> else {...}
> 
> expands to:
> 
> if (condition)
> if (1) {...}
> else ;
> else {...}

That is not ambiguous.

> And gcc doesn't like it:
> 
> unicodeobject.c: In function `PyUnicode_FromEncodedObject':
> unicodeobject.c:424: warning: suggest explicit braces to avoid ambiguous `else'
> unicodeobject.c:429: warning: suggest explicit braces to avoid ambiguous `else'
> unicodeobject.c: In function `PyUnicode_DecodeUTF8':
> unicodeobject.c:673: warning: suggest explicit braces to avoid ambiguous `else'
> unicodeobject.c:687: warning: suggest explicit braces to avoid ambiguous `else'
> unicodeobject.c:698: warning: suggest explicit braces to avoid ambiguous `else'
> unicodeobject.c:710: warning: suggest explicit braces to avoid ambiguous `else'
> unicodeobject.c:716: warning: suggest explicit braces to avoid ambiguous `else'
> unicodeobject.c: In function `PyUnicode_EncodeUTF8':
> unicodeobject.c:829: warning: suggest parentheses around arithmetic in operand of |
> unicodeobject.c: At top level:
> unicodeobject.c:755: warning: `utf8_encoding_error' defined but not used

Agreed. It produces warnings.

gcc is being stupid here because there is no ambiguity. It is saying "well,
gee golly, you might have a problem" but we really don't. gcc is trying to
be too smart.

But hey... if it wants to produce warnings... then fine. We can work around
the dumb thing.

> > All uses of the UTF8_ERROR() macro have a semicolon after them (like any
> > normal statement does(!)). This expands the macro to:
> > 
> >     if (1) {
> >        ...
> >     } else
> >        ;       /* this is the semicolon I referred to */
> > 
> > Therefore, it creates a complete if/else statement, and any surrounding
> > if/else statements will bind as expected.
> 
> The goto solution is much cleaner and also easier to understand.

Arguable.

> Please revert the change you made (or I will ;-).

I will, but only to shut gcc up. It is flat out wrong.

-g

-- 
Greg Stein, http://www.lyra.org/