[Ns-bugs] [Bug 1170] Formulate best practices for dealing with unused debug variables

code@nsnam.ece.gatech.edu code at nsnam.ece.gatech.edu
Mon Feb 6 11:41:19 PST 2012


https://www.nsnam.org/bugzilla/show_bug.cgi?id=1170

--- Comment #31 from Mathieu Lacage <mathieu.lacage at gmail.com> 2012-02-06 14:41:19 EST ---
(In reply to comment #28)
> (In reply to comment #26)
> > The obvious way to avoid the ifdef would be to use an explicit if statement to
> > wrap NS_ASSERT_MSG (the compiler will optimize away the empty statement), but,
> > really, NS_ASSERT_MSG should be doing this for me instead. 
> > 
> > In general, though, I believe that the debugging macros are just plain wrong
> > because they do not generate the code for the if statement unconditionally.
> > They should and then rely on the compiler to optimize stuff away. This is what
> > they are here for.
> > 
> > i.e., a patch to replace:
> > 
> > #else /* NS3_ASSERT_ENABLE */
> > #define NS_ASSERT(cond)
> > 
> > with:
> > #else /* NS3_ASSERT_ENABLE */
> > #define NS_ASSERT(condition)                                    \
> >   do                                                            \
> >     {                                                           \
> >       if (!(condition))                                         \
> >     {                                                       \
> >     }                                                       \
> >     }                                                           \
> >   while (false)
> > 
> > is pre-approved (same for log.h)
> > 
> > I am actually surprised that the code we have is like this because this is a
> > common thing for log/assert macros to do.
> 
> While this is a common trick, the compiler will produce code to evaluate the
> condition if it contains any function calls.
> And this contradicts to the statement that no debug code should execute in
> optimized build.

This is a great point. It took a bit of googling to finally come up with what
appears to be a common way to do this:
http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/

(I did discuss this specific hack on the gcc-help mailing-list where others
suggested the same approach)

i.e., 

(void)(sizeof(statement));

patches welcome.

-- 
Configure bugmail: https://www.nsnam.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.


More information about the Ns-bugs mailing list