]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/context.cc
Issue 2787: Sanitize usage of -DDEBUG, -DNDEBUG and assert
[lilypond.git] / lily / context.cc
index 5aacffc4eb552d8c77e17f6161dcf89b3604a7cc..348d307479d25a06b8df0250024c6582452f3cf3 100644 (file)
@@ -1,7 +1,7 @@
 /*
   This file is part of LilyPond, the GNU music typesetter.
 
-  Copyright (C) 2004--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
+  Copyright (C) 2004--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
 
   LilyPond is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -34,7 +34,7 @@
 bool
 Context::is_removable () const
 {
-  return context_list_ == SCM_EOL && ! client_count_
+  return scm_is_null (context_list_) && ! client_count_
          && !dynamic_cast<Global_context const *> (daddy_context_);
 }
 
@@ -56,11 +56,6 @@ Context::check_removal ()
     }
 }
 
-Context::Context (Context const & /* src */)
-{
-  assert (false);
-}
-
 Scheme_hash_table *
 Context::properties_dict () const
 {
@@ -186,7 +181,7 @@ Context::find_create_context (SCM n, const string &id, SCM operations)
   if (Context *existing = find_context_below (this, n, id))
     return existing;
 
-  if (n == ly_symbol2scm ("Bottom"))
+  if (scm_is_eq (n, ly_symbol2scm ("Bottom")))
     {
       Context *tg = get_default_interpreter (id);
       return tg;
@@ -231,14 +226,12 @@ Context::find_create_context (SCM n, const string &id, SCM operations)
   return ret;
 }
 
-IMPLEMENT_LISTENER (Context, acknowledge_infant);
 void
 Context::acknowledge_infant (SCM sev)
 {
   infant_event_ = Stream_event::unsmob (sev);
 }
 
-IMPLEMENT_LISTENER (Context, set_property_from_event);
 void
 Context::set_property_from_event (SCM sev)
 {
@@ -253,7 +246,7 @@ Context::set_property_from_event (SCM sev)
         unset_property (sym);
         return;
       }
-          
+
       bool ok = true;
       ok = type_check_assignment (sym, val, ly_symbol2scm ("translation-type?"));
 
@@ -262,7 +255,6 @@ Context::set_property_from_event (SCM sev)
     }
 }
 
-IMPLEMENT_LISTENER (Context, unset_property_from_event);
 void
 Context::unset_property_from_event (SCM sev)
 {
@@ -277,7 +269,6 @@ Context::unset_property_from_event (SCM sev)
   Creates a new context from a CreateContext event, and sends an
   AnnounceNewContext event to this context.
 */
-IMPLEMENT_LISTENER (Context, create_context_from_event);
 void
 Context::create_context_from_event (SCM sev)
 {
@@ -307,19 +298,19 @@ Context::create_context_from_event (SCM sev)
   /* We want to be the first ones to hear our own events. Therefore, wait
      before registering events_below_ */
   new_context->event_source ()->
-  add_listener (GET_LISTENER (new_context->create_context_from_event),
+  add_listener (new_context->GET_LISTENER (Context, create_context_from_event),
                 ly_symbol2scm ("CreateContext"));
   new_context->event_source ()->
-  add_listener (GET_LISTENER (new_context->remove_context),
+  add_listener (new_context->GET_LISTENER (Context, remove_context),
                 ly_symbol2scm ("RemoveContext"));
   new_context->event_source ()->
-  add_listener (GET_LISTENER (new_context->change_parent),
+  add_listener (new_context->GET_LISTENER (Context, change_parent),
                 ly_symbol2scm ("ChangeParent"));
   new_context->event_source ()->
-  add_listener (GET_LISTENER (new_context->set_property_from_event),
+  add_listener (new_context->GET_LISTENER (Context, set_property_from_event),
                 ly_symbol2scm ("SetProperty"));
   new_context->event_source ()->
-  add_listener (GET_LISTENER (new_context->unset_property_from_event),
+  add_listener (new_context->GET_LISTENER (Context, unset_property_from_event),
                 ly_symbol2scm ("UnsetProperty"));
 
   new_context->events_below_->register_as_listener (new_context->event_source_);
@@ -346,7 +337,7 @@ Context::path_to_acceptable_context (SCM name) const
   // but the Context_def expects to see elements of the form ('accepts symbol).
   SCM accepts = SCM_EOL;
   for (SCM s = definition_mods_; scm_is_pair (s); s = scm_cdr (s))
-    if (scm_caar (s) == ly_symbol2scm ("accepts"))
+    if (scm_is_eq (scm_caar (s), ly_symbol2scm ("accepts")))
       {
         SCM elt = scm_list_2 (scm_caar (s), scm_string_to_symbol (scm_cadar (s)));
         accepts = scm_cons (elt, accepts);
@@ -367,7 +358,7 @@ Context::create_context (Context_def *cdef,
   /* TODO: This is fairly misplaced. We can fix this when we have taken out all
      iterator specific stuff from the Context class */
   event_source_->
-  add_listener (GET_LISTENER (acknowledge_infant),
+  add_listener (GET_LISTENER (Context, acknowledge_infant),
                 ly_symbol2scm ("AnnounceNewContext"));
   /* The CreateContext creates a new context, and sends an announcement of the
      new context through another event. That event will be stored in
@@ -377,7 +368,7 @@ Context::create_context (Context_def *cdef,
                      ly_symbol2scm ("type"), cdef->get_context_name (),
                      ly_symbol2scm ("id"), ly_string2scm (id));
   event_source_->
-  remove_listener (GET_LISTENER (acknowledge_infant),
+  remove_listener (GET_LISTENER (Context, acknowledge_infant),
                    ly_symbol2scm ("AnnounceNewContext"));
 
   assert (infant_event_);
@@ -447,7 +438,7 @@ Context::get_default_interpreter (const string &context_id)
 Context *
 Context::where_defined (SCM sym, SCM *value) const
 {
-#ifndef NDEBUG
+#ifdef DEBUG
   if (profile_property_accesses)
     note_property_access (&context_property_lookup_table, sym);
 #endif
@@ -463,7 +454,7 @@ Context::where_defined (SCM sym, SCM *value) const
 bool
 Context::here_defined (SCM sym, SCM *value) const
 {
-#ifndef NDEBUG
+#ifdef DEBUG
   if (profile_property_accesses)
     note_property_access (&context_property_lookup_table, sym);
 #endif
@@ -477,7 +468,7 @@ Context::here_defined (SCM sym, SCM *value) const
 SCM
 Context::internal_get_property (SCM sym) const
 {
-#ifndef NDEBUG
+#ifdef DEBUG
   if (profile_property_accesses)
     note_property_access (&context_property_lookup_table, sym);
 #endif
@@ -519,7 +510,7 @@ Context::is_alias (SCM sym) const
   if (scm_is_eq (sym, context_name_symbol ()))
     return true;
 
-  return scm_c_memq (sym, aliases_) != SCM_BOOL_F;
+  return scm_is_true (scm_c_memq (sym, aliases_));
 }
 
 void
@@ -556,7 +547,6 @@ Context::unset_property (SCM sym)
   properties_dict ()->remove (sym);
 }
 
-IMPLEMENT_LISTENER (Context, change_parent);
 void
 Context::change_parent (SCM sev)
 {
@@ -570,7 +560,6 @@ Context::change_parent (SCM sev)
 /*
   Die. The next GC sweep should take care of the actual death.
  */
-IMPLEMENT_LISTENER (Context, remove_context);
 void
 Context::remove_context (SCM)
 {
@@ -663,55 +652,51 @@ Context::now_mom () const
 }
 
 int
-Context::print_smob (SCM s, SCM port, scm_print_state *)
+Context::print_smob (SCM port, scm_print_state *)
 {
-  Context *sc = (Context *) SCM_CELL_WORD_1 (s);
-
   scm_puts ("#<", port);
-  scm_puts (sc->class_name (), port);
-  if (Context_def *d = Context_def::unsmob (sc->definition_))
+  scm_puts (class_name (), port);
+  if (Context_def *d = Context_def::unsmob (definition_))
     {
       scm_puts (" ", port);
       scm_display (d->get_context_name (), port);
     }
 
-  if (!sc->id_string_.empty ())
+  if (!id_string_.empty ())
     {
       scm_puts ("=", port);
-      scm_puts (sc->id_string_.c_str (), port);
+      scm_puts (id_string_.c_str (), port);
     }
 
   scm_puts (" ", port);
 
-  scm_display (sc->context_list_, port);
+  scm_display (context_list_, port);
   scm_puts (" >", port);
 
   return 1;
 }
 
 SCM
-Context::mark_smob (SCM sm)
+Context::mark_smob ()
 {
-  Context *me = (Context *) SCM_CELL_WORD_1 (sm);
-
-  scm_gc_mark (me->context_list_);
-  scm_gc_mark (me->aliases_);
-  scm_gc_mark (me->definition_);
-  scm_gc_mark (me->definition_mods_);
-  scm_gc_mark (me->properties_scm_);
-  scm_gc_mark (me->accepts_list_);
-  scm_gc_mark (me->default_child_);
+  scm_gc_mark (context_list_);
+  scm_gc_mark (aliases_);
+  scm_gc_mark (definition_);
+  scm_gc_mark (definition_mods_);
+  scm_gc_mark (properties_scm_);
+  scm_gc_mark (accepts_list_);
+  scm_gc_mark (default_child_);
 
-  if (me->implementation_)
-    scm_gc_mark (me->implementation_->self_scm ());
+  if (implementation_)
+    scm_gc_mark (implementation_->self_scm ());
 
-  if (me->event_source_)
-    scm_gc_mark (me->event_source_->self_scm ());
+  if (event_source_)
+    scm_gc_mark (event_source_->self_scm ());
 
-  if (me->events_below_)
-    scm_gc_mark (me->events_below_->self_scm ());
+  if (events_below_)
+    scm_gc_mark (events_below_->self_scm ());
 
-  return me->properties_scm_;
+  return properties_scm_;
 }
 
 const char Context::type_p_name_[] = "ly:context?";
@@ -743,7 +728,7 @@ measure_length (Context const *context)
 {
   SCM l = context->get_property ("measureLength");
   Rational length (1);
-  if (Moment::unsmob (l))
+  if (Moment::is_smob (l))
     length = Moment::unsmob (l)->main_part_;
   return length;
 }
@@ -754,7 +739,7 @@ measure_position (Context const *context)
   SCM sm = context->get_property ("measurePosition");
 
   Moment m = 0;
-  if (Moment::unsmob (sm))
+  if (Moment::is_smob (sm))
     {
       m = *Moment::unsmob (sm);
 
@@ -812,14 +797,29 @@ set_context_property_on_children (Context *trans, SCM sym, SCM val)
 bool
 melisma_busy (Context *tr)
 {
-  SCM melisma_properties = tr->get_property ("melismaBusyProperties");
-  bool busy = false;
+  // When there are subcontexts, they are responsible for maintaining
+  // melismata.
+  SCM ch = tr->children_contexts ();
+  if (scm_is_pair (ch))
+    {
+      // all contexts need to have a busy melisma for this to evaluate
+      // to true.
+
+      do {
+        if (!melisma_busy (Context::unsmob (scm_car (ch))))
+          return false;
+        ch = scm_cdr (ch);
+      } while (scm_is_pair (ch));
+      return true;
+    }
 
-  for (; !busy && scm_is_pair (melisma_properties);
+  for (SCM melisma_properties = tr->get_property ("melismaBusyProperties");
+       scm_is_pair (melisma_properties);
        melisma_properties = scm_cdr (melisma_properties))
-    busy = busy || to_boolean (tr->internal_get_property (scm_car (melisma_properties)));
+    if (to_boolean (tr->get_property (scm_car (melisma_properties))))
+      return true;
 
-  return busy;
+  return false;
 }
 
 bool