]> git.donarmstrong.com Git - lilypond.git/commitdiff
Make ASSERT_LIVE_IS_ALLOWED() behave as a function
authorGraham Percival <gperciva@gperciva-desktop.(none)>
Sat, 9 Oct 2010 17:47:22 +0000 (18:47 +0100)
committerGraham Percival <gperciva@gperciva-desktop.(none)>
Sun, 10 Oct 2010 15:56:32 +0000 (16:56 +0100)
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

index 999f6cf231ea6c0c18f9782f9cb596c47e5286b1..152950563b5283d8be4d185093100be292e4f442 100644 (file)
@@ -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 */