Mailing List Archive

Support open source code!


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

Re: tlug: GCC compiler question



>>>>> "Peter" == Peter Linsley <plinsley@example.com> writes:

    Peter> It stems from the fact that the programmer requires that
    Peter> the macro is in it's own block; this could be inherent from
    Peter> the syntax of the macro or just that they want to deal with
    Peter> block scope.

Actually, the point is to conceal from surrounding code the fact that
the macro is a block.  Syntactically, it is now treated similarly to 
a call to a function returning void.  The error messages will be
different if used in an expression of course.

    Peter> Let's look at your discovery:

    Peter> #define spin_lock_init(x) do { (x)->lock = 0; } while(0)

    Peter> If it were used in the following way:

    Peter> if (true)
    Peter>      spin_lock_init (x);

    Peter> The semi-colon following "spin_lock_init (x);" will be
    Peter> consumed as part of the macro expansion as:

    Peter> if (true)
    Peter>    do { (x)->lock = 0; } while(0);

It's still there, not consumed, as far as I can see.

    Peter> If we were to declare the macro as follows:

    Peter> #define spin_lock_init(x) { (x)->lock = 0; }

    Peter> Then this would expand to

    Peter> if (true)
    Peter>    { (x)->lock = 0; }

    Peter> ...and not be syntactically correct, this will cause a
    Peter> compile error as no semi-colon is present.

This is exactly backward, isn't it?  The expansion would be

     if (true)
        { (x)->lock = 0; };  /* Warning Will Robinson! Extra semicolon!! */

and might or might not cause a syntax error depending on whether the
caller's intention was

     if (true)
        spin_lock_init(x);
     else
        { ... }

(which generates an "unmatched else" syntax error) or not.  Neither of 
Neil's alternatives work intuitively here.  A syntax directed editor
would have fits with

      if (true)
         spin_lock_init(x)  /* No semicolon!  ARRRGggh, gak, die! */
      else
         { ... }

>>>>> "Neil" == Neil Booth <NeilB@example.com> writes:

    Neil> or even

    Neil> #define spin_lock_init(x)       (x)->lock = 0;

    Neil> The only point I can see is causing compile-time errors for
    Neil> misuse, such as

    Neil>    z += spin_lock_init(x);

    Neil> which would compile under the 3rd alternative above. And who
    Neil> would really write such code anyway?

Somebody whose mouse slipped on delete-region, leaving the "z +=" part
hanging?  Someone who wrote something like

    z += (installing_module_p) ? 1 : spin_lock_init(x);

maybe?  Point is, in a large project, you don't know what people are
going to do with your functions.

-- 
University of Tsukuba                Tennodai 1-1-1 Tsukuba 305-8573 JAPAN
Institute of Policy and Planning Sciences        Tel/fax: +1 (298) 53-5091
__________________________________________________________________________
__________________________________________________________________________
What are those two straight lines for?  "XEmacs rules."
---------------------------------------------------------------
Next Nomikai: 20 November, 19:30 Tengu TokyoEkiMae 03-3275-3691
Next Meeting: 12 December, 12:30 Tokyo Station Yaesu central gate
---------------------------------------------------------------
Sponsor: PHT, makers of TurboLinux http://www.pht.co.jp


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links