From 15b45a0de620080a445727f9f29b40bd8286ca79 Mon Sep 17 00:00:00 2001 From: Graham Percival Date: Sat, 9 Oct 2010 18:47:22 +0100 Subject: [PATCH] Make ASSERT_LIVE_IS_ALLOWED() behave as a function David Kastrup helpfully pointed out that this macro expanded into multiple statements, whereas it looks like a function call. For example, something like this: if (condition) ASSERT_LIVE_IS_ALLOWED(); else could fail quite badly. The "do {...} while (0)" is an idiom that compilers optimize away, but allows the macro to behave as a function. --- lily/include/smobs.hh | 15 +++++++++------ 1 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lily/include/smobs.hh b/lily/include/smobs.hh index 999f6cf..1529505 100644 --- a/lily/include/smobs.hh +++ b/lily/include/smobs.hh @@ -156,13 +156,16 @@ extern bool parsed_objects_should_be_dead; #ifndef NDEBUG #define ASSERT_LIVE_IS_ALLOWED() \ - static bool passed_here_once;\ - if (parsed_objects_should_be_dead && !passed_here_once) { \ - ::programming_error (string ("Parsed object should be dead: ") + __PRETTY_FUNCTION__ ); \ - passed_here_once = true;\ - } + do { \ + static bool passed_here_once;\ + if (parsed_objects_should_be_dead && !passed_here_once) { \ + ::programming_error (string ("Parsed object should be dead: ") + __PRETTY_FUNCTION__ ); \ + passed_here_once = true;\ + } \ + while (0) #else -#define ASSERT_LIVE_IS_ALLOWED() {}; +#define ASSERT_LIVE_IS_ALLOWED() do { } \ + while (0) #endif #endif /* SMOBS_HH */ -- 1.6.0.4