From: Graham Percival Date: Sat, 9 Oct 2010 17:47:22 +0000 (+0100) Subject: Make ASSERT_LIVE_IS_ALLOWED() behave as a function X-Git-Tag: release/2.13.36-1~41 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=edd898943a27a824915e2a042633e23b32756ab7;p=lilypond.git 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. --- diff --git a/lily/include/smobs.hh b/lily/include/smobs.hh index 999f6cf231..152950563b 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 */