On Tuesday, 2018-01-16 19:38:25 +0000, Chuck Atkins wrote:
> > > +#define debug_printf(...) if(mesa_debug) { fprintf(stderr,
> > __VA_ARGS__); }
> >
> > `do {} while(0)`
> >
> 
> Forgive me but I don't understand, what you're saying here.

Sorry, I guess I could've used a few more characters to type my message :)

I meant that you could add a `do { X; } while(0)` loop around the `if`
in your macro, so as to avoid the common if/else bug, eg.:

  if (foo)
    debug_printf("foo");
  else
    debug_printf("not foo");

Without the `do{}while(0)` trick, this expands into:

  if (foo)
    if (mesa_debug)
    {
      fprintf(stderr, "foo");
    }
    else
      if (mesa_debug)
      {
        fprintf(stderr, "not foo");
      }

and you'll never print "not foo".

The usual fix for this class of bugs is to add the `do{}while(0)` loop
as follows:

  #define debug_printf(...) \
    do { \
      if (mesa_debug) { \
        fprintf(stderr, __VA_ARGS__); \
      } \
    } while(0)

> 
> 
> 
> > Why only replace some of the printfs?
> >
> 
> The fprintf's I replaced with debug_printf are informational status
> messages, while the ones I left are actual error messages.

My bad, I didn't actually look at them, just noticed they existed :)

> 
> - Chuck
_______________________________________________
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev

Reply via email to