Welcome to MSDN Blogs Sign in | Join | Help

Compiler nanny

This post reminded me on the case when C++ compiler warnings caused me more grief than benefits.

I spent some time yesterday making some [previously external] component to use asserts defined in VS headers instead of its own internal ones. Most of the time I wasted inventing ways to fool the compiler and to make trivial ASSERT definition to compile with -W4. The original definition looked like

#define ASSERT(x) do { if(x) { ... } } while(0)

Pretty standard stuff. However, compiler didn't like constants in conditionals and emitted a warning. Since VS build system enforced warning as errors, the warning would cause a build break. I didn't want to reduce warning level to 3 even temporarily. The solution was to declare external function and invoke it. The function had to be placed in a separate file so compiler wouldn't know that it always returned 0.

#define ASSERT(x) do { if(x) { ... } } while(AlwaysReturnsZero())

That fixed while(0). However, then statement

ASSERT(FALSE);

refused to compile since it turned into if(FALSE). Arrrrgh, compiler-nanny again. I hand to change the macro to

#define ASSERT(x) do { if( (x) | AlwaysReturnsZero() ) { ... } } while(AlwaysReturnsZero());

Now it compiles fine.

Bottom line: I love stick shifts. No tiptronic will ever come close :-)

 

Published Thursday, July 22, 2004 3:43 PM by Mikhail Arkhipov (MSFT)

Comments

# re: Yet another reason I hate C

Thursday, July 22, 2004 6:45 PM by Cyrus' Blather

# re: Compiler nanny

Maybe you guys should implement _Pragma from C99 finally so you can temporarily disable this warning in macros.
Friday, July 23, 2004 2:16 AM by asdf

# re: Compiler nanny

In fact, there is a pragma for that in the compiler, but build system may have it disabled since otherwise people may start adding disabling pragmas and we end up with poor code which just seems to be compiling under W4.
Friday, July 23, 2004 11:31 AM by Mikhail Arkhipov (MSFT)

# re: Compiler nanny

Michael: Can't you just Push/Pop away that warning?

Also, stick shifts suck. I know, I've got one. You need to drive a real car to realize it though :-)
Wednesday, August 04, 2004 3:05 AM by Cyrus Najmabadi

# Compiler nanny, take 2.

Thursday, August 05, 2004 12:00 PM by Mikhail Arkhipov (MSFT)'s WebLog
New Comments to this post are disabled
 
Page view tracker