lkml.org 
[lkml]   [2004]   [Feb]   [6]   [last100]   RSS Feed
Views: [more markup]  [less markup]  [headers]  [forward] 
 
Messages in this thread
/
SubjectRe: [Infiniband-general] Getting an Infiniband access layer in theLinux kernel
FromRoland Dreier <>
Date06 Feb 2004 09:23:00 -0800
    Richard> If some major changes are being considered, I think it's
    Richard> time to get rid of the:

    Richard> do { } while(0) stuff that permiates a lot of MACROS and
    Richard> just use the { } as they were designed.
    Richard> Before everybody screams, think. It's perfectly correct
    Richard> to start a new "program unit" without a conditional
    Richard> expression.  You just add a curley-brace, then close the
    Richard> brace when you are though.

This is totally, totally wrong.  If you get rid of do { } while (0),
then you can't use the macro in an if statement.  Read any C FAQ for
details, or try the following:
    #define MAC(x) { x = x + 1; }
    int main() {
      int x = 0;
      if (1)
        MAC(x);
      else
        x = x - 1;
    }
I get the following (correct) error:

    $ gcc a.c
    a.c: In function `main':
    a.c:8: syntax error before "else"
    $ gcc --version
    gcc (GCC) 3.2.3 20030502 (Red Hat Linux 3.2.3-20)

because

    if (1)
        { x = x + 1 } ; /* <-- note semicolon
    else
        x = x - 1;
is not correct C.

By the way, it is possible to use parentheses and commas for some
simple macros, so for example the following is OK:

    #define MAC(x) ( x = x + 1, x = x * 2 )

    int main() {
      int x = 0;
      if (1)
        MAC(x);
      else
        x = x - 1;
    }
However I don't see anything wrong with the perfectly standard "do { }
while (0)" idiom.  Certainly if some compiler generates worse code for
that construct that just a plain { }, _that_ is a compiler bug that we
shouldn't have to work around.
 - Roland
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2005-03-22 14:00    [W:0.170 / U:0.440 seconds]
©2003-2008 Jasper Spaans