19

A customer recently performed static analysis of my employer's C codebase and gave us the results. Among useful patches was the request to change the famous do { ... } while(0) macro to do { ... } while(0,0). I understand what their patch is doing (using the sequence operator to return evaluate to the value of the second "0", so the effect is the same) but it's not clear why they'd favor the second form over the first form.

Is there a legitimate reason why one should prefer the second form of the macro, or is our customer's static analysis being overly pedantic?

9
  • 5
    Your customer's static analysis tool was being overly something, that's for sure.
    – Chris Lutz
    Commented Dec 5, 2009 at 22:46
  • 7
    I'd go for "too overly redundant". Commented Dec 5, 2009 at 22:47
  • 4
    I can't think of any reason to do this. Perhaps some tool complains with a while(0) condition along the lines of "this will never happen" but any such tool should 1) recognize that this is a very common idiom in C with macros and 2) differentiate between do...while(0) and while(0){}. Have you asked them for a rationale? Commented Dec 5, 2009 at 22:50
  • 21
    Yeah, (0,0) looks like an owl. Maybe they thought your code needs more owls. Commented Dec 5, 2009 at 22:54
  • 7
    Well in C and C++, 0,0 is not a constant expression, but 0 is. So it's more likely that compilers warn about the latter than the former. But it's also more likely compilers optimize away the latter than the former - so i don't quite see what they win when changing it. Commented Dec 6, 2009 at 9:35

3 Answers 3

17

Just a guess as to why they might suggest using

do { ... } while(0,0)

over

do { ... } while(0)

Even though there's no behavior difference and should be no runtime cost difference between the two.

My guess is that the static analysis tool complains about the while loop being controlled by a constant in the simpler case and doesn't when 0,0 is used. The customer's suggestion is probably just so they don't get a bunch of false positives from the tool.

For example I occasionally come across situations where I want to have a conditional statement controlled by a constant, but the compiler will complain with a warning about a conditional expression evaluating to a constant. Then I have to jump through some hoops to get the compiler to stop complaining (since I don't like to have spurious warnings).

Your customer's suggestion is one of the hoops I've used to quiet that warning, though in my case it wasn't controlling a while loop, it was to deal with an "always fails" assertion. Occasionally, I'll have an area of code that should never execute (maybe the default case of a switch). In that situation I might have an assertion that always fails with some message:

assert( !"We should have never gotten here, dammit...");

But, at least one compiler I use issues a warning about the expression always evaluating to false. However, if I change it to:

assert( ("We should have never gotten here, dammit...", 0));

The warning goes away, and everybody's happy. I'm guessing that even your customer's static analysis tool would be, too. Note that I generally hide that bit of hoop jumping behind a macro like:

#define ASSERT_FAIL( x) assert( ((x), 0))

It might be nice to be able to tell the tool vendor to fix the problem, but there might be legitimate cases where they actually do want to diagnose a loop being controlled by a constant boolean expression. Not to mention the fact that even if you convince a tool vendor to make such a change, that doesn't help you for the next year or so that it might take to actually get a fix.

3
  • 2
    +1 This might be it, but if this is the case these warnings should just be disabled with command line options. Why jump through hoops when you can just turn off the hoops that aren't necessary?
    – Chris Lutz
    Commented Dec 6, 2009 at 9:28
  • 2
    Well, for one, futzing around with compiler or tool options might be considered a form of hoop jumping. Screwing around with the makefiles or whatever controls your build is often much more black magic than C preprocessor hackery is (and that's saying a lot). For another, you might not want the warning to be disabled for everything - maybe just for the debugging macros that seem to need these stupid tricks. Commented Dec 6, 2009 at 9:54
  • 2
    And now my static analysis complains, "Redundant left argument to comma." Sometimes you just can't win.
    – John
    Commented Mar 6, 2014 at 22:26
13

Using while(0,0) prevents Microsoft compiler from generating a warning about a condition which is a constant (Warning C4127).

When this warning is enabled (e.g. with /W4 or /Wall), it can be shut down on a case by case basis with this nice little trick (see this other thread).

EDIT: Since Visual Studio 2017 15.3, while(0) does not emit warnings anymore (cf. Constant Conditionals). You can get rid of your (0,0) !

3
  • 3
    For some reason I am not surprised by this.
    – user1143634
    Commented Nov 6, 2018 at 12:50
  • Quick note that the Constant Conditionals URL currently redirects to devblogs.microsoft.com/cppblog/… - it might be worth updating the link when the edit queues are clear.
    – AJM
    Commented Feb 12 at 17:43
  • 1
    @AJM Just did it.
    – Arnaud
    Commented Feb 15 at 7:37
11

Well, I'll go for an answer:

Is there a legitimate reason why one should prefer the second form of the macro... ?

No. There is no legitimate reason. Both always evaluate to false, and any decent compiler will probably turn the second one into the first in the assembly anyway. If there was any reason for it to be invalid in some cases, C's been around far long enough for that reason to be discovered by greater gurus than I.

If you like your code making owl-y eyes at you, use while(0,0). Otherwise, use what the rest of the C programming world uses and tell your customer's static analysis tool to shove it.

13
  • It does not evaluate to anything; and do-loop is not an expression and has no value. Try assigning it int x = macro() ;, and it fails to compile.
    – Clifford
    Commented Dec 5, 2009 at 22:58
  • 1
    I'm referring to the evaluation of 0 vs. 0,0, not the do { } while(0) loop in general.
    – Chris Lutz
    Commented Dec 5, 2009 at 23:00
  • 1
    "What benefit?": The benefit conveyed by using 0,0 in the while loop condition. The static analysis seems to believe there is one, but this answer, though accepted does not reveal that so we are left wondering.
    – Clifford
    Commented Dec 6, 2009 at 8:35
  • 1
    @Chris: I get your point, but out of curiosity, I was hoping that someone could enlighten us as to why the tool was advising this - even if the thinking were flawed. I can find no reference to such an idiom anywhere. Even teh MISRA C guidelines do not advise this. It would help if the OP were to tell us what the tool was perhaps?
    – Clifford
    Commented Dec 7, 2009 at 10:34
  • 3
    0,0 makes VC++ warning 4127 go away. That's the legitimate reason this pattern exists. If you think 0,0 is an unacceptable hack, then why do you think using do{}while(0) itself isn't a worse hack?
    – ThreeBit
    Commented Apr 2, 2014 at 22:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.