On Fri, Oct 8, 2010 at 6:35 AM, David Kastrup <[email protected]> wrote:
> Graham Percival <[email protected]> writes:
>> in that the macro might look like a single statement, but it gets
>> expanded into two. (is it worth enclosing the non-else definition
>> with a { } to avoid this?)
>
> "{ ... }" is not sufficient since that already is a complete statement
> without adding a semicolon, unlike a function call.
>
> The wrapper needs to be "do { ... } while (0)". It is an idiom that
> compilers optimize away.
Ok, how's this patch?
Cheers,
- Graham
From 15b45a0de620080a445727f9f29b40bd8286ca79 Mon Sep 17 00:00:00 2001
From: Graham Percival <gperc...@gperciva-desktop.(none)>
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
_______________________________________________
lilypond-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/lilypond-devel