freetype-devel
[Top][All Lists]
Advanced

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

Re: [ft-devel] MSVC build woes !


From: Robin Watts
Subject: Re: [ft-devel] MSVC build woes !
Date: Tue, 11 Jun 2013 15:14:06 +0100
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130509 Thunderbird/17.0.6

On 11/06/2013 13:08, John Emmas wrote:
#define FT_ASSERT( condition  )                               \
do                                                            \
{                                                             \
    if ( !( condition ) )                                     \
        FT_Panic( "assertion failed on line %d of file %s\n", \
                  __LINE__, __FILE__ );                       \
} while ( 0 )

It usually gets called something like this:-

           FT_ASSERT( some_var == some_other_var );

and wherever it gets called, I get warning 4127.

Indeed.

MSVC is warning that  there's no point putting this in a do/while loop,
since nothing ever changes.  Rather than re-instate the pragma, a
better solution would be to remove 'do' and 'while'.

No, no, no! Absolutely wrong!

There is a VERY good reason for the while being there. I will try to explain:

Imagine that you have the following code:

  if (x == 0)
      FT_ASSERT(some_condition);
  else
      return 42;
  return 23;

Without the while loop, the code would expand to:

   if (x == 0)
      if (!some_condition)
          FT_Panic( ... );
      else
          return 42;
   return 23;

(indentation changed for clarity)

i.e. the behaviour of the code is changed for x == 0; in the first case, the assert would be tested, then 23 would be returned. In the second the assert would be tested, then 42 would be returned.

By putting the do/while loop in, you avoid the possibility of the macro changing the surrounding control flow.

When writing macros in C it is considered good style to try to minimise unintended consequences of the macros use (a good example of "The Principle of Least Surprise").

At first glance, it doesn't look like
there'd be any adverse effects.  If you agree that removing 'do' and
'while' is the way forward, I'll find you the other occurrences.  Regards,

Those examples of do and while are put there very deliberately. Removing them would be a harmful step.

It is true that the MSVC warning is unhelpful, so finding a way to stop it (such as by putting the original set of lines back in) would be preferable.

Robin




reply via email to

[Prev in Thread] Current Thread [Next in Thread]