bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] build: fix build of variables.c


From: Eric Blake
Subject: Re: [PATCH] build: fix build of variables.c
Date: Fri, 17 Apr 2015 11:49:36 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0

On 04/17/2015 11:42 AM, Eric Blake wrote:
> On 04/17/2015 11:38 AM, Eric Blake wrote:
>> On 04/17/2015 11:01 AM, Chet Ramey wrote:
>>> On 4/17/15 10:26 AM, Eric Blake wrote:
>>>> gcc  -DPROGRAM='"bash"' -DCONF_HOSTTYPE='"x86_64"' 
>>>> -DCONF_OSTYPE='"linux-gnu"' -DCONF_MACHTYPE='"x86_64-unknown-linux-gnu"' 
>>>> -DCONF_VENDOR='"unknown"' -DLOCALEDIR='"/usr/local/share/locale"' 
>>>> -DPACKAGE='"bash"' -DSHELL -DHAVE_CONFIG_H -DDEBUG -DMALLOC_DEBUG -I.  -I. 
>>>> -I./include -I./lib   -g -O2 -Wno-parentheses -Wno-format-security -c 
>>>> variables.c
>>>> variables.c: In function ‘initialize_shell_variables’:
>>>> variables.c:425:7: error: ‘else’ without a previous ‘if’
>>>>        else
>>>>               ^
>>>
>>> What non-default options did you enable when you attempted the build?
>>
>> None that I recall (merely git checkout, ./configure; make).  Maybe it
>> had something to do with the fact that I had been incrementally building
>> from earlier points in the tree, as a fresh checkout doesn't seem to
>> reproduce the problem?
> 
> At any rate, even if I can't easily reproduce the setup, it's fairly
> obvious that if FUNCTION_IMPORT is undefined, as well as either
> ARRAY_VARS or ARRAY_EXPORT, that you indeed do get an unbalanced 'else'
> unless you group the 'else' to go inside the '#if' that introduces the
> 'if'.  So even if I can't provide an easy formula for reproduction, the
> patch should still be valid.

And as a matter of style, it goes to show why in my own code, I much
prefer using preprocessor conditionals to set witnesses to '0' or '1'
outside of function bodies, and avoiding #if inside the body by instead
relying on the compiler to optimize out 'if (0)' branches while still
validating that code will still compile, without having to figure out
what combination of preprocessor knobs will get to a certain expansion
of code.

That is, I find:

#ifdef ARRAY_VARS
# define USE_ARRAY 1
#else
# define USE_ARRAY 0
#endif
void foo(...)
{
  if (USE_ARRAY && cond)
    {
      ...
    }
  else
    {
      ...
    }
}

easier to maintain than:

void foo(...)
{
#ifdef ARRAY_VARS
  if (cond)
    {
      ...
    }
  else
#endif
    {
      ...
    }
}

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]