# HG changeset patch # User Andrey Mazo # Date 1312214119 -14400 # Node ID 9fa9d2c76996e52eea5199ca0204f798f3344e42 # Parent 200fa0ae38c5608beb93ae5bcbd72b941f2b5156 Bug 1170: Formulate best practices for dealing with unused debug variables. diff --git a/examples/energy/energy-model-example.cc b/examples/energy/energy-model-example.cc --- a/examples/energy/energy-model-example.cc +++ b/examples/energy/energy-model-example.cc @@ -43,6 +43,7 @@ void ReceivePacket (Ptr socket) { +#ifdef NS3_LOG_ENABLE Ptr packet; Address from; while (packet = socket->RecvFrom (from)) @@ -53,11 +54,11 @@ NS_LOG_UNCOND ("--\nReceived one packet! Socket: "<< iaddr.GetIpv4 () << " port: " << iaddr.GetPort () << " at time = " << Simulator::Now ().GetSeconds () << "\n--"); - //cast iaddr to void, to suppress 'iaddr' set but not used compiler warning - //in optimized builds - (void) iaddr; } } +#else + while (socket->Recv ()); +#endif } /** diff --git a/examples/wireless/multirate.cc b/examples/wireless/multirate.cc --- a/examples/wireless/multirate.cc +++ b/examples/wireless/multirate.cc @@ -313,33 +313,26 @@ Experiment::ApplicationSetup (Ptr client, Ptr server, double start, double stop) { - Vector serverPos = GetPosition (server); - Vector clientPos = GetPosition (client); + NS_LOG_ONLY_ASSIGNMENT (Vector serverPos, GetPosition (server)); + NS_LOG_ONLY_ASSIGNMENT (Vector clientPos, GetPosition (client)); Ptr ipv4Server = server->GetObject(); Ptr ipv4Client = client->GetObject(); Ipv4InterfaceAddress iaddrServer = ipv4Server->GetAddress (1,0); - Ipv4InterfaceAddress iaddrClient = ipv4Client->GetAddress (1,0); + NS_LOG_ONLY_ASSIGNMENT (Ipv4InterfaceAddress iaddrClient, ipv4Client->GetAddress (1,0)); Ipv4Address ipv4AddrServer = iaddrServer.GetLocal (); - Ipv4Address ipv4AddrClient = iaddrClient.GetLocal (); + NS_LOG_ONLY_ASSIGNMENT (Ipv4Address ipv4AddrClient, iaddrClient.GetLocal ()); NS_LOG_DEBUG ("Set up Server Device " << (server->GetDevice (0))->GetAddress () << " with ip " << ipv4AddrServer << " position (" << serverPos.x << "," << serverPos.y << "," << serverPos.z << ")"); - NS_LOG_DEBUG ("Set up Client Device " << (client->GetDevice (0))->GetAddress () << " with ip " << ipv4AddrClient << " position (" << clientPos.x << "," << clientPos.y << "," << clientPos.z << ")" << "\n"); - //cast serverPos,clientPos,iaddrClient to void, to suppress variable set but not - //used compiler warning in optimized builds - (void) serverPos; - (void) clientPos; - (void) ipv4AddrClient; - // Equipping the source node with OnOff Application used for sending OnOffHelper onoff ("ns3::UdpSocketFactory", Address (InetSocketAddress (Ipv4Address ("10.0.0.1"), port))); onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1))); diff --git a/examples/wireless/wifi-simple-interference.cc b/examples/wireless/wifi-simple-interference.cc --- a/examples/wireless/wifi-simple-interference.cc +++ b/examples/wireless/wifi-simple-interference.cc @@ -98,13 +98,12 @@ void ReceivePacket (Ptr socket) { +#ifdef NS3_LOG_ENABLE Address addr; socket->GetSockName (addr); InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr); NS_LOG_UNCOND ("Received one packet! Socket: " << iaddr.GetIpv4 () << " port: " << iaddr.GetPort ()); - //cast iaddr to void, to suppress iaddr set but not used compiler warning - //in optimized builds - (void) iaddr; +#endif } static void GenerateTraffic (Ptr socket, uint32_t pktSize, diff --git a/src/applications/model/packet-sink.cc b/src/applications/model/packet-sink.cc --- a/src/applications/model/packet-sink.cc +++ b/src/applications/model/packet-sink.cc @@ -165,13 +165,10 @@ if (InetSocketAddress::IsMatchingType (from)) { m_totalRx += packet->GetSize (); - InetSocketAddress address = InetSocketAddress::ConvertFrom (from); + NS_LOG_ONLY_ASSIGNMENT (InetSocketAddress address, InetSocketAddress::ConvertFrom (from)); NS_LOG_INFO ("Received " << packet->GetSize () << " bytes from " << address.GetIpv4 () << " [" << address << "]" << " total Rx " << m_totalRx); - //cast address to void , to suppress 'address' set but not used - //compiler warning in optimized builds - (void) address; } m_rxTrace (packet, from); } diff --git a/src/applications/model/v4ping.cc b/src/applications/model/v4ping.cc --- a/src/applications/model/v4ping.cc +++ b/src/applications/model/v4ping.cc @@ -235,15 +235,11 @@ m_socket->SetAttribute ("Protocol", UintegerValue (1)); // icmp m_socket->SetRecvCallback (MakeCallback (&V4Ping::Receive, this)); InetSocketAddress src = InetSocketAddress (Ipv4Address::GetAny (), 0); - int status; - status = m_socket->Bind (src); + NS_ASSERT_ASSIGNMENT (int status, m_socket->Bind (src)); NS_ASSERT (status != -1); InetSocketAddress dst = InetSocketAddress (m_remote, 0); - status = m_socket->Connect (dst); + NS_ASSERT_ASSIGNMENT (status, m_socket->Connect (dst)); NS_ASSERT (status != -1); - //cast status to void, to suppress 'status' set but not used compiler warning - //in optimized builds - (void) status; Send (); } diff --git a/src/click/helper/click-internet-stack-helper.cc b/src/click/helper/click-internet-stack-helper.cc --- a/src/click/helper/click-internet-stack-helper.cc +++ b/src/click/helper/click-internet-stack-helper.cc @@ -449,9 +449,9 @@ // be aggregated to the same node. // Ptr ipv4L3Protocol = ipv4->GetObject (); - bool __attribute__ ((unused)) result = ipv4L3Protocol->TraceConnectWithoutContext ("Drop", - MakeBoundCallback (&Ipv4L3ProtocolDropSinkWithoutContext, - theStream)); + NS_ASSERT_ASSIGNMENT (bool result, ipv4L3Protocol->TraceConnectWithoutContext ("Drop", + MakeBoundCallback (&Ipv4L3ProtocolDropSinkWithoutContext, + theStream))); NS_ASSERT_MSG (result == true, "ClickInternetStackHelper::EanableAsciiIpv4Internal(): " "Unable to connect ipv4L3Protocol \"Drop\""); } diff --git a/src/core/examples/main-callback.cc b/src/core/examples/main-callback.cc --- a/src/core/examples/main-callback.cc +++ b/src/core/examples/main-callback.cc @@ -32,11 +32,9 @@ // this is not a null callback NS_ASSERT (!one.IsNull ()); // invoke cbOne function through callback instance - double retOne; - retOne = one (10.0, 20.0); - // cast retOne to void, to suppress variable ‘retOne’ set but - // not used compiler warning - (void) retOne; + NS_ASSERT_ASSIGNMENT (double retOne, one (10.0, 20.0)); + // callback returned expected value + NS_ASSERT (retOne == 10.0); // return type: int // first arg type: double @@ -47,11 +45,10 @@ // this is not a null callback NS_ASSERT (!two.IsNull ()); // invoke MyCb::cbTwo through callback instance - int retTwo; - retTwo = two (10.0); - // cast retTwo to void, to suppress variable ‘retTwo’ set but - // not used compiler warning - (void) retTwo; + NS_ASSERT_ASSIGNMENT (int retTwo, two (10.0)); + // callback returned expected value + NS_ASSERT (retTwo == -5); + two = MakeNullCallback (); // invoking a null callback is just like // invoking a null function pointer: diff --git a/src/core/model/assert.h b/src/core/model/assert.h --- a/src/core/model/assert.h +++ b/src/core/model/assert.h @@ -95,10 +95,34 @@ } \ while (false) +/** + * \ingroup assert + * \param var the variable to assign to + * \param value the value which to assign + * + * Construct a simple assignment (var = value) if NS3_ASSERT_ENABLE is true (debugging builds). + * Expands to (value) otherwise. + * This is useful to avoid "set but not used" compiler warnings in non-debugging builds. + */ +#define NS_ASSERT_ASSIGNMENT(var, value) \ + var = (value) + +/** + * \ingroup assert + * \param var the variable to assign to + * \param value the value which to assign + * + * Similar to NS_ASSERT_ASSIGNMENT() but expands to absolutely nothing in non-debugging builds. + */ +#define NS_ASSERT_ONLY_ASSIGNMENT(var, value) \ + NS_ASSERT_ASSIGNMENT (var, value) + #else /* NS3_ASSERT_ENABLE */ #define NS_ASSERT(cond) #define NS_ASSERT_MSG(cond,msg) +#define NS_ASSERT_ASSIGNMENT(var, value) (value) +#define NS_ASSERT_ONLY_ASSIGNMENT(var, value) #endif /* NS3_ASSERT_ENABLE */ diff --git a/src/core/model/log.h b/src/core/model/log.h --- a/src/core/model/log.h +++ b/src/core/model/log.h @@ -323,6 +323,28 @@ } \ while (false) +/** + * \ingroup logging + * \param var the variable to assign to + * \param value the value which to assign + * + * Construct a simple assignment if NS3_LOG_ENABLE is true (debugging builds). + * Expands to (value) otherwise. + * This is useful to avoid "set but not used" compiler warnings in non-debugging builds. + */ +#define NS_LOG_ASSIGNMENT(var, value) \ + var = (value) + +/** + * \ingroup logging + * \param var the variable to assign to + * \param value the value which to assign + * + * Similar to NS_LOG_ASSIGNMENT() but expands to absolutely nothing in non-debugging builds. + */ +#define NS_LOG_ONLY_ASSIGNMENT(var, value) \ + NS_LOG_ASSIGNMENT (var, value) + #else /* LOG_ENABLE */ #define NS_LOG_COMPONENT_DEFINE(component) @@ -335,6 +357,8 @@ #define NS_LOG_FUNCTION(msg) #define NS_LOG_LOGIC(msg) #define NS_LOG_UNCOND(msg) +#define NS_LOG_ASSIGNMENT(var, value) (value) +#define NS_LOG_ONLY_ASSIGNMENT(var, value) #endif /* LOG_ENABLE */ diff --git a/src/core/model/unused.h b/src/core/model/unused.h new file mode 100644 --- /dev/null +++ b/src/core/model/unused.h @@ -0,0 +1,8 @@ +#ifndef UNUSED_H +#define UNUSED_H + +#ifndef NS_UNUSED +# define NS_UNUSED(x) (void)(x) +#endif + +#endif /* UNUSED_H */ diff --git a/src/core/wscript b/src/core/wscript --- a/src/core/wscript +++ b/src/core/wscript @@ -238,6 +238,7 @@ 'model/vector.h', 'model/default-deleter.h', 'model/fatal-impl.h', + 'model/unused.h', ] if sys.platform == 'win32': diff --git a/src/flow-monitor/model/ipv4-flow-probe.cc b/src/flow-monitor/model/ipv4-flow-probe.cc --- a/src/flow-monitor/model/ipv4-flow-probe.cc +++ b/src/flow-monitor/model/ipv4-flow-probe.cc @@ -328,12 +328,9 @@ Ipv4FlowProbeTag fTag; // ConstCast: see http://www.nsnam.org/bugzilla/show_bug.cgi?id=904 - bool tagFound; - tagFound = ConstCast (ipPayload)->RemovePacketTag (fTag); + NS_ASSERT_ASSIGNMENT (bool tagFound, ConstCast (ipPayload)->RemovePacketTag (fTag)); NS_ASSERT_MSG (tagFound, "FlowProbeTag is missing"); - // cast tagFound to void, to suppress 'tagFound' set but not used compiler - // warning in optimized builds - (void) tagFound; + FlowId flowId = fTag.GetFlowId (); FlowPacketId packetId = fTag.GetPacketId (); uint32_t size = fTag.GetPacketSize (); diff --git a/src/internet/helper/internet-stack-helper.cc b/src/internet/helper/internet-stack-helper.cc --- a/src/internet/helper/internet-stack-helper.cc +++ b/src/internet/helper/internet-stack-helper.cc @@ -485,16 +485,13 @@ NS_ASSERT_MSG (ipv4L3Protocol, "InternetStackHelper::EnablePcapIpv4Internal(): " "m_ipv4Enabled and ipv4L3Protocol inconsistent"); - bool result = ipv4L3Protocol->TraceConnectWithoutContext ("Tx", MakeCallback (&Ipv4L3ProtocolRxTxSink)); + NS_ASSERT_ASSIGNMENT (bool result, ipv4L3Protocol->TraceConnectWithoutContext ("Tx", MakeCallback (&Ipv4L3ProtocolRxTxSink))); NS_ASSERT_MSG (result == true, "InternetStackHelper::EnablePcapIpv4Internal(): " "Unable to connect ipv4L3Protocol \"Tx\""); - result = ipv4L3Protocol->TraceConnectWithoutContext ("Rx", MakeCallback (&Ipv4L3ProtocolRxTxSink)); + NS_ASSERT_ASSIGNMENT (result, ipv4L3Protocol->TraceConnectWithoutContext ("Rx", MakeCallback (&Ipv4L3ProtocolRxTxSink))); NS_ASSERT_MSG (result == true, "InternetStackHelper::EnablePcapIpv4Internal(): " "Unable to connect ipv4L3Protocol \"Rx\""); - // cast result to void, to suppress ‘result’ set but not used compiler-warning - // for optimized builds - (void) result; } g_interfaceFileMapIpv4[std::make_pair (ipv4, interface)] = file; @@ -580,16 +577,13 @@ NS_ASSERT_MSG (ipv6L3Protocol, "InternetStackHelper::EnablePcapIpv6Internal(): " "m_ipv6Enabled and ipv6L3Protocol inconsistent"); - bool result = ipv6L3Protocol->TraceConnectWithoutContext ("Tx", MakeCallback (&Ipv6L3ProtocolRxTxSink)); + NS_ASSERT_ASSIGNMENT (bool result, ipv6L3Protocol->TraceConnectWithoutContext ("Tx", MakeCallback (&Ipv6L3ProtocolRxTxSink))); NS_ASSERT_MSG (result == true, "InternetStackHelper::EnablePcapIpv6Internal(): " "Unable to connect ipv6L3Protocol \"Tx\""); - result = ipv6L3Protocol->TraceConnectWithoutContext ("Rx", MakeCallback (&Ipv6L3ProtocolRxTxSink)); + NS_ASSERT_ASSIGNMENT (result, ipv6L3Protocol->TraceConnectWithoutContext ("Rx", MakeCallback (&Ipv6L3ProtocolRxTxSink))); NS_ASSERT_MSG (result == true, "InternetStackHelper::EnablePcapIpv6Internal(): " "Unable to connect ipv6L3Protocol \"Rx\""); - // cast found to void, to suppress ‘result’ set but not used compiler-warning - // for optimized builds - (void) result; } g_interfaceFileMapIpv6[std::make_pair (ipv6, interface)] = file; @@ -742,8 +736,8 @@ // be aggregated to the same node. // Ptr ipv4L3Protocol = ipv4->GetObject (); - bool __attribute__ ((unused)) result = ipv4L3Protocol->TraceConnectWithoutContext ("Drop", - MakeBoundCallback (&Ipv4L3ProtocolDropSinkWithoutContext, theStream)); + NS_ASSERT_ASSIGNMENT (bool result, ipv4L3Protocol->TraceConnectWithoutContext ("Drop", + MakeBoundCallback (&Ipv4L3ProtocolDropSinkWithoutContext, theStream))); NS_ASSERT_MSG (result == true, "InternetStackHelper::EanableAsciiIpv4Internal(): " "Unable to connect ipv4L3Protocol \"Drop\""); } @@ -929,8 +923,8 @@ // be aggregated to the same node. // Ptr ipv6L3Protocol = ipv6->GetObject (); - bool __attribute__ ((unused)) result = ipv6L3Protocol->TraceConnectWithoutContext ("Drop", - MakeBoundCallback (&Ipv6L3ProtocolDropSinkWithoutContext, theStream)); + NS_ASSERT_ASSIGNMENT (bool result, ipv6L3Protocol->TraceConnectWithoutContext ("Drop", + MakeBoundCallback (&Ipv6L3ProtocolDropSinkWithoutContext, theStream))); NS_ASSERT_MSG (result == true, "InternetStackHelper::EnableAsciiIpv6Internal(): " "Unable to connect ipv6L3Protocol \"Drop\""); } diff --git a/src/internet/model/tcp-socket-base.cc b/src/internet/model/tcp-socket-base.cc --- a/src/internet/model/tcp-socket-base.cc +++ b/src/internet/model/tcp-socket-base.cc @@ -413,12 +413,8 @@ if (packet != 0 && packet->GetSize () != 0) { SocketAddressTag tag; - bool found; - found = packet->PeekPacketTag (tag); + NS_ASSERT_ASSIGNMENT (bool found, packet->PeekPacketTag (tag)); NS_ASSERT (found); - //cast found to void , to suppress 'found' set but not used compiler warning - //in optimized builds - (void) found; fromAddress = tag.GetAddress (); } return packet; diff --git a/src/internet/model/udp-socket-impl.cc b/src/internet/model/udp-socket-impl.cc --- a/src/internet/model/udp-socket-impl.cc +++ b/src/internet/model/udp-socket-impl.cc @@ -544,12 +544,8 @@ if (packet != 0) { SocketAddressTag tag; - bool found; - found = packet->PeekPacketTag (tag); + NS_ASSERT_ASSIGNMENT (bool found, packet->PeekPacketTag (tag)); NS_ASSERT (found); - //cast found to void, to suppress 'found' set but not used,compiler warning - //in optimized builds - (void) found; fromAddress = tag.GetAddress (); } return packet; diff --git a/src/internet/test/ipv4-raw-test.cc b/src/internet/test/ipv4-raw-test.cc --- a/src/internet/test/ipv4-raw-test.cc +++ b/src/internet/test/ipv4-raw-test.cc @@ -106,28 +106,20 @@ void Ipv4RawSocketImplTest::ReceivePkt (Ptr socket) { - uint32_t availableData; - availableData = socket->GetRxAvailable (); + NS_ASSERT_ONLY_ASSIGNMENT (uint32_t availableData, socket->GetRxAvailable ()); m_receivedPacket = socket->Recv (2, MSG_PEEK); NS_ASSERT (m_receivedPacket->GetSize () == 2); m_receivedPacket = socket->Recv (std::numeric_limits::max (), 0); NS_ASSERT (availableData == m_receivedPacket->GetSize ()); - //cast availableData to void, to suppress 'availableData' set but not used - //compiler warning - (void) availableData; } void Ipv4RawSocketImplTest::ReceivePkt2 (Ptr socket) { - uint32_t availableData; - availableData = socket->GetRxAvailable (); + NS_ASSERT_ONLY_ASSIGNMENT (uint32_t availableData, socket->GetRxAvailable ()); m_receivedPacket2 = socket->Recv (2, MSG_PEEK); NS_ASSERT (m_receivedPacket2->GetSize () == 2); m_receivedPacket2 = socket->Recv (std::numeric_limits::max (), 0); NS_ASSERT (availableData == m_receivedPacket2->GetSize ()); - //cast availableData to void, to suppress 'availableData' set but not used - //compiler warning - (void) availableData; } void diff --git a/src/internet/test/udp-test.cc b/src/internet/test/udp-test.cc --- a/src/internet/test/udp-test.cc +++ b/src/internet/test/udp-test.cc @@ -90,13 +90,9 @@ void UdpSocketLoopbackTest::ReceivePkt (Ptr socket) { - uint32_t availableData; - availableData = socket->GetRxAvailable (); + NS_ASSERT_ONLY_ASSIGNMENT (uint32_t availableData, socket->GetRxAvailable ()); m_receivedPacket = socket->Recv (std::numeric_limits::max (), 0); NS_ASSERT (availableData == m_receivedPacket->GetSize ()); - //cast availableData to void, to suppress 'availableData' set but not used - //compiler warning - (void) availableData; } void @@ -151,24 +147,16 @@ void UdpSocketImplTest::ReceivePkt (Ptr socket) { - uint32_t availableData; - availableData = socket->GetRxAvailable (); + NS_ASSERT_ONLY_ASSIGNMENT (uint32_t availableData, socket->GetRxAvailable ()); m_receivedPacket = socket->Recv (std::numeric_limits::max (), 0); NS_ASSERT (availableData == m_receivedPacket->GetSize ()); - //cast availableData to void, to suppress 'availableData' set but not used - //compiler warning - (void) availableData; } void UdpSocketImplTest::ReceivePkt2 (Ptr socket) { - uint32_t availableData; - availableData = socket->GetRxAvailable (); + NS_ASSERT_ONLY_ASSIGNMENT (uint32_t availableData, socket->GetRxAvailable ()); m_receivedPacket2 = socket->Recv (std::numeric_limits::max (), 0); NS_ASSERT (availableData == m_receivedPacket2->GetSize ()); - //cast availableData to void, to suppress 'availableData' set but not used - //compiler warning - (void) availableData; } void diff --git a/src/mesh/model/dot11s/peer-management-protocol.cc b/src/mesh/model/dot11s/peer-management-protocol.cc --- a/src/mesh/model/dot11s/peer-management-protocol.cc +++ b/src/mesh/model/dot11s/peer-management-protocol.cc @@ -383,11 +383,7 @@ UniformVariable beaconShift (-m_maxBeaconShift, m_maxBeaconShift); PeerLinksMap::iterator iface = m_peerLinks.find (interface); NS_ASSERT (iface != m_peerLinks.end ()); - PeerManagementProtocolMacMap::const_iterator plugin = m_plugins.find (interface); - NS_ASSERT (plugin != m_plugins.end ()); - // cast plugin to void, to suppress 'plugin' set but not used, compiler warning - // in optimized builds - (void) plugin; + NS_ASSERT (m_plugins.find (interface) != m_plugins.end ()); std::map::const_iterator lastBeacon = m_lastBeacon.find (interface); std::map::const_iterator beaconInterval = m_beaconInterval.find (interface); if ((lastBeacon == m_lastBeacon.end ()) || (beaconInterval == m_beaconInterval.end ())) diff --git a/src/mobility/helper/ns2-mobility-helper.cc b/src/mobility/helper/ns2-mobility-helper.cc --- a/src/mobility/helper/ns2-mobility-helper.cc +++ b/src/mobility/helper/ns2-mobility-helper.cc @@ -42,6 +42,7 @@ #include #include #include "ns3/log.h" +#include "ns3/unused.h" #include "ns3/simulator.h" #include "ns3/node-list.h" #include "ns3/node.h" @@ -400,8 +401,7 @@ { char *endp; double v = strtod (s.c_str (), &endp); // declared with warn_unused_result - //cast v to void, to suppress v set but not used compiler warning - (void) v; + NS_UNUSED (v); // suppress "set but not used" compiler warning return endp == s.c_str () + s.size (); } diff --git a/src/network/helper/trace-helper.h b/src/network/helper/trace-helper.h --- a/src/network/helper/trace-helper.h +++ b/src/network/helper/trace-helper.h @@ -94,8 +94,9 @@ template void PcapHelper::HookDefaultSink (Ptr object, std::string tracename, Ptr file) { - bool __attribute__ ((unused)) result = - object->TraceConnectWithoutContext (tracename.c_str (), MakeBoundCallback (&DefaultSink, file)); + NS_ASSERT_ASSIGNMENT (bool result, + object->TraceConnectWithoutContext (tracename.c_str (), + MakeBoundCallback (&DefaultSink, file))); NS_ASSERT_MSG (result == true, "PcapHelper::HookDefaultSink(): Unable to hook \"" << tracename << "\""); } @@ -231,8 +232,9 @@ template void AsciiTraceHelper::HookDefaultEnqueueSinkWithoutContext (Ptr object, std::string tracename, Ptr file) { - bool __attribute__ ((unused)) result = - object->TraceConnectWithoutContext (tracename, MakeBoundCallback (&DefaultEnqueueSinkWithoutContext, file)); + NS_ASSERT_ASSIGNMENT (bool result, + object->TraceConnectWithoutContext (tracename, + MakeBoundCallback (&DefaultEnqueueSinkWithoutContext, file))); NS_ASSERT_MSG (result == true, "AsciiTraceHelper::HookDefaultEnqueueSinkWithoutContext(): Unable to hook \"" << tracename << "\""); } @@ -244,8 +246,10 @@ std::string tracename, Ptr stream) { - bool __attribute__ ((unused)) result = - object->TraceConnect (tracename, context, MakeBoundCallback (&DefaultEnqueueSinkWithContext, stream)); + NS_ASSERT_ASSIGNMENT (bool result, + object->TraceConnect (tracename, + context, + MakeBoundCallback (&DefaultEnqueueSinkWithContext, stream))); NS_ASSERT_MSG (result == true, "AsciiTraceHelper::HookDefaultEnqueueSinkWithContext(): Unable to hook \"" << tracename << "\""); } @@ -253,8 +257,9 @@ template void AsciiTraceHelper::HookDefaultDropSinkWithoutContext (Ptr object, std::string tracename, Ptr file) { - bool __attribute__ ((unused)) result = - object->TraceConnectWithoutContext (tracename, MakeBoundCallback (&DefaultDropSinkWithoutContext, file)); + NS_ASSERT_ASSIGNMENT (bool result, + object->TraceConnectWithoutContext (tracename, + MakeBoundCallback (&DefaultDropSinkWithoutContext, file))); NS_ASSERT_MSG (result == true, "AsciiTraceHelper::HookDefaultDropSinkWithoutContext(): Unable to hook \"" << tracename << "\""); } @@ -266,8 +271,10 @@ std::string tracename, Ptr stream) { - bool __attribute__ ((unused)) result = - object->TraceConnect (tracename, context, MakeBoundCallback (&DefaultDropSinkWithContext, stream)); + NS_ASSERT_ASSIGNMENT (bool result, + object->TraceConnect (tracename, + context, + MakeBoundCallback (&DefaultDropSinkWithContext, stream))); NS_ASSERT_MSG (result == true, "AsciiTraceHelper::HookDefaultDropSinkWithContext(): Unable to hook \"" << tracename << "\""); } @@ -275,8 +282,9 @@ template void AsciiTraceHelper::HookDefaultDequeueSinkWithoutContext (Ptr object, std::string tracename, Ptr file) { - bool __attribute__ ((unused)) result = - object->TraceConnectWithoutContext (tracename, MakeBoundCallback (&DefaultDequeueSinkWithoutContext, file)); + NS_ASSERT_ASSIGNMENT (bool result, + object->TraceConnectWithoutContext (tracename, + MakeBoundCallback (&DefaultDequeueSinkWithoutContext, file))); NS_ASSERT_MSG (result == true, "AsciiTraceHelper::HookDefaultDequeueSinkWithoutContext(): Unable to hook \"" << tracename << "\""); } @@ -288,8 +296,10 @@ std::string tracename, Ptr stream) { - bool __attribute__ ((unused)) result = - object->TraceConnect (tracename, context, MakeBoundCallback (&DefaultDequeueSinkWithContext, stream)); + NS_ASSERT_ASSIGNMENT (bool result, + object->TraceConnect (tracename, + context, + MakeBoundCallback (&DefaultDequeueSinkWithContext, stream))); NS_ASSERT_MSG (result == true, "AsciiTraceHelper::HookDefaultDequeueSinkWithContext(): Unable to hook \"" << tracename << "\""); } @@ -297,8 +307,9 @@ template void AsciiTraceHelper::HookDefaultReceiveSinkWithoutContext (Ptr object, std::string tracename, Ptr file) { - bool __attribute__ ((unused)) result = - object->TraceConnectWithoutContext (tracename, MakeBoundCallback (&DefaultReceiveSinkWithoutContext, file)); + NS_ASSERT_ASSIGNMENT (bool result, + object->TraceConnectWithoutContext (tracename, + MakeBoundCallback (&DefaultReceiveSinkWithoutContext, file))); NS_ASSERT_MSG (result == true, "AsciiTraceHelper::HookDefaultReceiveSinkWithoutContext(): Unable to hook \"" << tracename << "\""); } @@ -310,8 +321,10 @@ std::string tracename, Ptr stream) { - bool __attribute__ ((unused)) result = - object->TraceConnect (tracename, context, MakeBoundCallback (&DefaultReceiveSinkWithContext, stream)); + NS_ASSERT_ASSIGNMENT (bool result, + object->TraceConnect (tracename, + context, + MakeBoundCallback (&DefaultReceiveSinkWithContext, stream))); NS_ASSERT_MSG (result == true, "AsciiTraceHelper::HookDefaultReceiveSinkWithContext(): Unable to hook \"" << tracename << "\""); } diff --git a/src/network/model/socket.cc b/src/network/model/socket.cc --- a/src/network/model/socket.cc +++ b/src/network/model/socket.cc @@ -304,19 +304,16 @@ { if (netdevice != 0) { - bool found = false; + NS_ASSERT_ONLY_ASSIGNMENT (bool found, false); for (uint32_t i = 0; i < GetNode ()->GetNDevices (); i++) { if (GetNode ()->GetDevice (i) == netdevice) { - found = true; + NS_ASSERT_ONLY_ASSIGNMENT (found, true); break; } } NS_ASSERT_MSG (found, "Socket cannot be bound to a NetDevice not existing on the Node"); - //cast found to void, to suppress 'found' set but not used compiler warning - //in optimized builds - (void) found; } m_boundnetdevice = netdevice; return; diff --git a/src/network/utils/packet-socket.cc b/src/network/utils/packet-socket.cc --- a/src/network/utils/packet-socket.cc +++ b/src/network/utils/packet-socket.cc @@ -438,12 +438,8 @@ if (packet != 0) { SocketAddressTag tag; - bool found; - found = packet->PeekPacketTag (tag); + NS_ASSERT_ASSIGNMENT (bool found, packet->PeekPacketTag (tag)); NS_ASSERT (found); - //cast found to void, to suppress 'found' set but not used compiler warning - //in optimized builds - (void) found; fromAddress = tag.GetAddress (); } return packet; diff --git a/src/network/utils/radiotap-header.cc b/src/network/utils/radiotap-header.cc --- a/src/network/utils/radiotap-header.cc +++ b/src/network/utils/radiotap-header.cc @@ -134,8 +134,11 @@ { NS_LOG_FUNCTION (this); - uint8_t __attribute__ ((unused)) tmp = start.ReadU8 (); // major version of radiotap header - NS_ASSERT_MSG (tmp == 0x00, "RadiotapHeader::Deserialize(): Unexpected major version"); +#ifdef NS3_ASSERT_ENABLE + NS_ASSERT_MSG (start.ReadU8 () == 0x00, "RadiotapHeader::Deserialize(): Unexpected major version"); // major version of radiotap header +#else + start.Next (); // major version of radiotap header +#endif start.ReadU8 (); // pad field m_length = start.ReadU16 (); // entire length of radiotap data + header diff --git a/src/test/ns3wifi/wifi-interference-test-suite.cc b/src/test/ns3wifi/wifi-interference-test-suite.cc --- a/src/test/ns3wifi/wifi-interference-test-suite.cc +++ b/src/test/ns3wifi/wifi-interference-test-suite.cc @@ -80,13 +80,12 @@ void WifiInterferenceTestCase::ReceivePacket (Ptr socket) { +#ifdef NS3_LOG_ENABLE Address addr; socket->GetSockName (addr); InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr); NS_LOG_UNCOND ("Received one packet! Socket: " << iaddr.GetIpv4 () << " port: " << iaddr.GetPort ()); - //cast iaddr to void, to suppress 'iaddr' set but not used compiler warning - //in optimized builds - (void) iaddr; +#endif } void diff --git a/src/topology-read/model/rocketfuel-topology-reader.cc b/src/topology-read/model/rocketfuel-topology-reader.cc --- a/src/topology-read/model/rocketfuel-topology-reader.cc +++ b/src/topology-read/model/rocketfuel-topology-reader.cc @@ -25,6 +25,7 @@ #include #include "ns3/log.h" +#include "ns3/unused.h" #include "rocketfuel-topology-reader.h" namespace ns3 { @@ -84,8 +85,10 @@ std::string ptr; std::string name; std::string nuid; +#ifdef NS3_LOG_ENABLE bool dns = false; bool bb = false; +#endif int num_neigh_s = 0; unsigned int num_neigh = 0; int radius = 0; @@ -95,6 +98,7 @@ uid = argv[0]; loc = argv[1]; +#ifdef NS3_LOG_ENABLE if (argv[2]) { dns = true; @@ -104,6 +108,7 @@ { bb = true; } +#endif num_neigh_s = ::atoi (argv[4]); if (num_neigh_s < 0) @@ -156,11 +161,6 @@ << "(" << "%d" << ") externals: \"%s\"(%d) " << "name: " << name << " radius: " << radius); - //cast bb and dns to void, to suppress variable set but not used compiler warning - //in optimized builds - (void) bb; - (void) dns; - // Create node and link if (!uid.empty ()) { @@ -209,8 +209,7 @@ sname = argv[0]; tname = argv[1]; double v = strtod (argv[2], &endptr); // weight - // cast v to void , to suppress 'v' set but not used compiler warning - (void) v; + NS_UNUSED (v); // suppress "set but not used" compiler warning in optimized builds if (*endptr != '\0') { NS_LOG_WARN ("invalid weight: " << argv[2]); diff --git a/src/wifi/model/wifi-information-element.cc b/src/wifi/model/wifi-information-element.cc --- a/src/wifi/model/wifi-information-element.cc +++ b/src/wifi/model/wifi-information-element.cc @@ -50,14 +50,11 @@ Buffer::Iterator WifiInformationElement::Deserialize (Buffer::Iterator i) { - Buffer::Iterator start = i; + NS_ASSERT_ONLY_ASSIGNMENT (Buffer::Iterator start, i); i = DeserializeIfPresent (i); // This IE was not optional, so confirm that we did actually // deserialise something. NS_ASSERT (i.GetDistanceFrom (start) != 0); - // cast start to void, to suppress ‘start’ set but not used - // compiler warning in optimized builds - (void) start; return i; } diff --git a/src/wifi/model/wifi-remote-station-manager.cc b/src/wifi/model/wifi-remote-station-manager.cc --- a/src/wifi/model/wifi-remote-station-manager.cc +++ b/src/wifi/model/wifi-remote-station-manager.cc @@ -365,12 +365,8 @@ if (!IsLowLatency ()) { TxModeTag tag; - bool found; - found = ConstCast (packet)->PeekPacketTag (tag); + NS_ASSERT_ASSIGNMENT (bool found, ConstCast (packet)->PeekPacketTag (tag)); NS_ASSERT (found); - // cast found to void, to suppress 'found' set but not used - // compiler warning - (void) found; return tag.GetDataMode (); } return DoGetDataMode (Lookup (address, header), fullPacketSize); @@ -383,12 +379,8 @@ if (!IsLowLatency ()) { TxModeTag tag; - bool found; - found = ConstCast (packet)->PeekPacketTag (tag); + NS_ASSERT_ASSIGNMENT (bool found, ConstCast (packet)->PeekPacketTag (tag)); NS_ASSERT (found); - // cast found to void, to suppress 'found' set but not used - // compiler warning - (void) found; return tag.GetRtsMode (); } return DoGetRtsMode (Lookup (address, header));