[Ns-bugs] [Bug 1170] Formulate best practices for dealing with unused debug variables
code@nsnam.ece.gatech.edu
code at nsnam.ece.gatech.edu
Tue Feb 7 03:49:13 PST 2012
https://www.nsnam.org/bugzilla/show_bug.cgi?id=1170
--- Comment #33 from Andrey Mazo <mazo at iitp.ru> 2012-02-07 06:49:13 EST ---
(In reply to comment #32)
> > 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.
> Wow, it's a very nice trick I haven't seen before.
>
> Unfortunately, Intel C++ Compiler gives
> "remark #593: variable "b" was set but never used"
> for the code:
> """
> #define MyAssert(x) do { (void)sizeof(x); } while (0)
>
> int DoJob (int a);
>
> void qqq (int a)
> {
> int b = DoJob (a);
> MyAssert (b);
> }
> """
> But it's not a supported platform, so we can ignore it.
>
> Anyway this assert implementation is better than that we have now.
Hmm. the downside of this implementation is that it silences compiler even when
it shouldn't.
For example, (from src/internet/test/ipv4-raw-test.cc)
"""
void Ipv4RawSocketImplTest::ReceivePkt (Ptr<Socket> socket)
{
uint32_t availableData;
availableData = socket->GetRxAvailable ();
m_receivedPacket = socket->Recv (2, MSG_PEEK);
NS_ASSERT (m_receivedPacket->GetSize () == 2);
m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
NS_ASSERT (availableData == m_receivedPacket->GetSize ());
}
"""
This will successfully compile (with NS_ASSERT() implemented as sizeof()) and
emit the call to GetRxAvailable().
But the call to GetRxAvailable() is needed only for debug build.
The only solution is to mark GetRxAvailable() with attribute pure or const, but
I suppose, that it's again gcc-specific.
So in this particular case, I'd prefer the old NS_ASSERT() implementation which
will trigger "set but not used" warning.
--
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