diff --git a/samples/main-debug.cc b/samples/main-debug.cc index 396fcdb0c..cf2ab1a29 100644 --- a/samples/main-debug.cc +++ b/samples/main-debug.cc @@ -22,6 +22,6 @@ int main (int argc, int argv) NS_ASSERT (a == 0); NS_ASSERT_MSG (a == 0, "my msg"); - NS_ASSERT (a != 0) + NS_ASSERT (a != 0); NS_ASSERT_MSG (a != 0, "my 2 msg"); } diff --git a/src/core/assert.h b/src/core/assert.h index bf3fed648..36a036aae 100644 --- a/src/core/assert.h +++ b/src/core/assert.h @@ -60,14 +60,19 @@ void AssertBreakpoint (void); * unverified condition and halts in the ns3::AssertBreakpoint * function. */ -#define NS_ASSERT(condition) \ - if (!(condition)) \ +#define NS_ASSERT(condition) \ + do \ { \ - std::cout << "assert failed. file=" << __FILE__ << \ - ", line=" << __LINE__ << ", cond=\""#condition << \ - "\"" << std::endl; \ - ns3::AssertBreakpoint (); \ - } + if (!(condition)) \ + { \ + std::cout << "assert failed. file=" << __FILE__ << \ + ", line=" << __LINE__ << ", cond=\""#condition << \ + "\"" << std::endl; \ + ns3::AssertBreakpoint (); \ + } \ + } \ + while (false) + /** * \ingroup assert @@ -78,12 +83,16 @@ void AssertBreakpoint (void); * true, the program prints the message to output and * halts in the ns3::AssertBreakpoint function. */ -#define NS_ASSERT_MSG(condition, message) \ - if (!(condition)) \ - { \ - std::cout << message << std::endl; \ - ns3::AssertBreakpoint (); \ - } +#define NS_ASSERT_MSG(condition, message) \ + do \ + { \ + if (!(condition)) \ + { \ + std::cout << message << std::endl; \ + ns3::AssertBreakpoint (); \ + } \ + } \ + while (false) #else /* NS3_ASSERT_ENABLE */ diff --git a/src/core/fatal-error.h b/src/core/fatal-error.h index f2bba0f84..f5020b054 100644 --- a/src/core/fatal-error.h +++ b/src/core/fatal-error.h @@ -36,8 +36,12 @@ * builds. */ #define NS_FATAL_ERROR(msg) \ - std::cout << msg << std::endl; \ - ns3::AssertBreakpoint (); + do \ + { \ + std::cout << msg << std::endl; \ + ns3::AssertBreakpoint (); \ + } \ + while (false) #endif /* FATAL_ERROR_H */