groff-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[groff] 08/48: [troff]: Improve assertions.


From: G. Branden Robinson
Subject: [groff] 08/48: [troff]: Improve assertions.
Date: Sun, 28 Apr 2024 23:06:16 -0400 (EDT)

gbranden pushed a commit to branch master
in repository groff.

commit 739577d90dde4d9124dd59d979001e8a6c401e6c
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Wed Apr 24 15:41:39 2024 -0500

    [troff]: Improve assertions.
    
    * src/roff/troff/env.cpp (tab_stops::to_string)
      (environment::wrap_up_tab, environment::handle_tab):
    * src/roff/troff/input.cpp (process_input_stack, read_size,
      (token::process, main):
    * src/roff/troff/node.cpp (font_info::get_tfont)
      (kern_pair_node::ends_sentence, node_list_ends_sentence, node::split)
       unbreakable_space_node::nbreaks):
    * src/roff/troff/reg.cpp (number_value_to_ascii): Replace `assert(0)`
      calls with communicative predicates.
    
    * src/roff/troff/number.cpp (get_vunits, get_hunits, get_number)
      (get_integer, is_valid_expression, is_valid_term): Align language of
      assertion failures with others in use.
    
    This eliminates the last of the `assert(0)` calls in GNU troff (the
    program itself; we still have some in output drivers, preprocessors,
    utilities,and libgroff).
    
    Also put space between control flow keywords and subsequent
    parenthesized expressions.  Things that aren't function calls shouldn't
    look like function calls (and this is more consistent with the
    prevailing code style anyway).
---
 ChangeLog                 | 16 ++++++++++++++++
 src/roff/troff/env.cpp    | 12 ++++++------
 src/roff/troff/input.cpp  | 34 +++++++++++++++++-----------------
 src/roff/troff/node.cpp   | 10 +++++-----
 src/roff/troff/number.cpp | 12 ++++++------
 src/roff/troff/reg.cpp    |  4 ++--
 6 files changed, 52 insertions(+), 36 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 309d88e3a..1763f32d8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2024-04-24  G. Branden Robinson <g.branden.robinson@gmail.com>
+
+       * src/roff/troff/env.cpp (tab_stops::to_string)
+       (environment::wrap_up_tab, environment::handle_tab):
+       * src/roff/troff/input.cpp (process_input_stack, read_size)
+       (token::process, main)
+       * src/roff/troff/node.cpp (font_info::get_tfont)
+       (kern_pair_node::ends_sentence, node_list_ends_sentence)
+       (node::split, unbreakable_space_node::nbreaks):
+       * src/roff/troff/reg.cpp (number_value_to_ascii):
+       Replace `assert(0)` calls with communicative predicates.
+
+       * src/roff/troff/number.cpp (get_vunits, get_hunits, get_number)
+       (get_integer, is_valid_expression, is_valid_term): Align
+       language of assertion failures with others in use.
+
 2024-04-24  G. Branden Robinson <g.branden.robinson@gmail.com>
 
        * src/roff/troff/env.cpp (environment::print_env): Fix glitch in
diff --git a/src/roff/troff/env.cpp b/src/roff/troff/env.cpp
index 1c6bb8f11..29c833049 100644
--- a/src/roff/troff/env.cpp
+++ b/src/roff/troff/env.cpp
@@ -2216,7 +2216,7 @@ void environment::possibly_break_line(int start_here, int 
forced)
     bp->nd->split(bp->index, &pre, &post);
     *ndp = post;
     hunits extra_space_width = H0;
-    switch(adjust_mode) {
+    switch (adjust_mode) {
     case ADJUST_BOTH:
       if (bp->nspaces != 0)
        extra_space_width = target_text_length - bp->width;
@@ -2770,7 +2770,7 @@ const char *tab_stops::to_string()
       break;
     case TAB_NONE:
     default:
-      assert(0);
+      assert(0 == "unhandled case of `p->type` (tab_type)");
     }
   }
   if (repeated_list)
@@ -2791,7 +2791,7 @@ const char *tab_stops::to_string()
       break;
     case TAB_NONE:
     default:
-      assert(0);
+      assert(0 == "unhandled case of `p->type` (tab_type)");
     }
   }
   *ptr++ = '\0';
@@ -2968,7 +2968,7 @@ void environment::wrap_up_tab()
   case TAB_NONE:
   case TAB_LEFT:
   default:
-    assert(0);
+    assert(0 == "unhandled case of `current_tab` (tab_type)");
   }
   width_total += tab_amount;
   width_total += tab_width;
@@ -3032,7 +3032,7 @@ void environment::handle_tab(int is_leader)
     add_node(make_tag("tab C", absolute.to_units()));
     break;
   default:
-    assert(0);
+    assert(0 == "unhandled case of `t` (tab_type)");
   }
   tab_width = 0;
   tab_distance = d;
@@ -3644,7 +3644,7 @@ static void print_hyphenation_exceptions()
   // space; see `hyphen_trie::read_patterns_file()`.
   const size_t bufsz = WORD_MAX * 2;
   char wordbuf[bufsz];
-  while(iter.get(&entry, reinterpret_cast<void **>(&hypoint))) {
+  while (iter.get(&entry, reinterpret_cast<void **>(&hypoint))) {
     assert(!entry.is_null());
     assert(hypoint != 0 /* nullptr */);
     string word = entry.contents();
diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp
index db61117ea..8b0f02935 100644
--- a/src/roff/troff/input.cpp
+++ b/src/roff/troff/input.cpp
@@ -1102,7 +1102,7 @@ static int get_copy(node **nd, bool is_defining, bool 
handle_escape_E)
       return c;
   again:
     c = input_stack::peek();
-    switch(c) {
+    switch (c) {
     case 0:
       return escape_char;
     case '"':
@@ -1546,7 +1546,7 @@ static void report_color()
   dictionary_iterator iter(color_dictionary);
   symbol key;
   color *value;
-  while(iter.get(&key, reinterpret_cast<void **>(&value))) {
+  while (iter.get(&key, reinterpret_cast<void **>(&value))) {
     assert(!key.is_null());
     assert(value != 0 /* nullptr */);
     errprint("%1\n", key.contents());
@@ -1883,7 +1883,7 @@ void token::next()
     int cc = input_stack::get(&n);
     if (cc != escape_char || escape_char == 0) {
     handle_ordinary_char:
-      switch(cc) {
+      switch (cc) {
       case INPUT_NO_BREAK_SPACE:
          type = TOKEN_STRETCHABLE_SPACE;
          return;
@@ -2060,7 +2060,7 @@ void token::next()
     else {
     handle_escape_char:
       cc = input_stack::get(&n);
-      switch(cc) {
+      switch (cc) {
       case '(':
        nm = read_two_char_escape_parameter();
        type = TOKEN_SPECIAL;
@@ -2458,7 +2458,7 @@ int token::operator==(const token &t)
 {
   if (type != t.type)
     return 0;
-  switch(type) {
+  switch (type) {
   case TOKEN_CHAR:
     return c == t.c;
   case TOKEN_SPECIAL:
@@ -2482,7 +2482,7 @@ int token::operator!=(const token &t)
 // doesn't tokenize it) and accepts a user-specified delimiter.
 static bool is_char_usable_as_delimiter(int c)
 {
-  switch(c) {
+  switch (c) {
   case '0':
   case '1':
   case '2':
@@ -2516,7 +2516,7 @@ static bool is_char_usable_as_delimiter(int c)
 bool token::is_usable_as_delimiter(bool report_error)
 {
   bool is_valid = false;
-  switch(type) {
+  switch (type) {
   case TOKEN_CHAR:
     is_valid = is_char_usable_as_delimiter(c);
     if (!is_valid && report_error)
@@ -3055,7 +3055,7 @@ void process_input_stack()
          break;
 #endif /* COLUMN */
        default:
-         assert(0);
+         assert(0 == "unhandled case of `request_code` (int)");
          break;
        }
        suppress_next = 1;
@@ -4203,7 +4203,7 @@ bool is_codepoint_composite(const char *n)
   dictionary_iterator iter(composite_dictionary);
   symbol key;
   char *value;
-  while(iter.get(&key, reinterpret_cast<void **>(&value))) {
+  while (iter.get(&key, reinterpret_cast<void **>(&value))) {
     assert(!key.is_null());
     assert(value != 0 /* nullptr */);
     const char *k = key.contents();
@@ -4225,7 +4225,7 @@ static void report_composite_characters()
   dictionary_iterator iter(composite_dictionary);
   symbol key;
   char *value;
-  while(iter.get(&key, reinterpret_cast<void **>(&value))) {
+  while (iter.get(&key, reinterpret_cast<void **>(&value))) {
     assert(!key.is_null());
     assert(value != 0 /* nullptr */);
     const char *k = key.contents();
@@ -5325,7 +5325,7 @@ static bool read_size(int *x)
       *x = curenv->get_requested_point_size() - val;
       break;
     default:
-      assert(0);
+      assert(0 == "unhandled case of type size increment operator");
     }
     if (*x <= 0) {
       warning(WARN_RANGE,
@@ -5898,7 +5898,7 @@ static void skip_branch()
     else if (c == ESCAPE_RIGHT_BRACE)
       --level;
     else if (c == escape_char && escape_char > 0)
-      switch(input_stack::get(0)) {
+      switch (input_stack::get(0)) {
       case '{':
        ++level;
        break;
@@ -7604,10 +7604,10 @@ void token::process()
     curenv->add_node(new dummy_node);
     break;
   case TOKEN_EMPTY:
-    assert(0);
+    assert(0 == "unhandled empty token");
     break;
   case TOKEN_EOF:
-    assert(0);
+    assert(0 == "unhandled end-of-file token");
     break;
   case TOKEN_ESCAPE:
     if (escape_char != 0)
@@ -7685,7 +7685,7 @@ void token::process()
       break;
     }
   default:
-    assert(0);
+    assert(0 == "unhandled token type");
   }
 }
 
@@ -8285,7 +8285,7 @@ int main(int argc, char **argv)
                          "abciI:vw:W:zCEf:m:n:o:r:d:F:M:T:tqs:RU"
                          DEBUG_OPTION, long_options, 0))
         != EOF)
-    switch(c) {
+    switch (c) {
     case 'v':
       {
        printf("GNU troff (groff) version %s\n", Version_string);
@@ -8396,7 +8396,7 @@ int main(int argc, char **argv)
       exit(1);
       break;           // never reached
     default:
-      assert(0);
+      assert(0 == "unhandled case of command-line option");
     }
   if (want_unsafe_requests)
     mac_path = &macro_path;
diff --git a/src/roff/troff/node.cpp b/src/roff/troff/node.cpp
index c2e029899..cc11507e1 100644
--- a/src/roff/troff/node.cpp
+++ b/src/roff/troff/node.cpp
@@ -323,7 +323,7 @@ tfont *font_info::get_tfont(font_size fs, int height, int 
slant, int fontno)
                    36*72*sizescale);
          break;
        default:
-         assert(0);
+         assert(0 == "unhandled case of constant spacing mode");
        }
        if (fontno != number)
          return make_tfont(spec);
@@ -3324,7 +3324,7 @@ int kern_pair_node::ends_sentence()
   case 2:
     break;
   default:
-    assert(0);
+    assert(0 == "unhandled case of sentence ending status");
   }
   return n1->ends_sentence();
 }
@@ -3340,7 +3340,7 @@ int node_list_ends_sentence(node *n)
     case 2:
       break;
     default:
-      assert(0);
+      assert(0 == "unhandled case of sentence ending status");
     }
   return 2;
 }
@@ -3735,7 +3735,7 @@ int dbreak_node::nbreaks()
 
 void node::split(int /*where*/, node ** /*prep*/, node ** /*postp*/)
 {
-  assert(0);
+  assert(0 == "node::split() unimplemented");
 }
 
 void space_node::split(int where, node **pre, node **post)
@@ -4489,7 +4489,7 @@ int unbreakable_space_node::nbreaks()
 
 void unbreakable_space_node::split(int, node **, node **)
 {
-  assert(0);
+  assert(0 == "unbreakable_space_node::split() unimplemented");
 }
 
 int unbreakable_space_node::merge_space(hunits, hunits, hunits)
diff --git a/src/roff/troff/number.cpp b/src/roff/troff/number.cpp
index e6960a3af..e5fc83d5b 100644
--- a/src/roff/troff/number.cpp
+++ b/src/roff/troff/number.cpp
@@ -126,7 +126,7 @@ bool get_vunits(vunits *res, unsigned char si, vunits 
prev_value)
     *res = prev_value - v;
     break;
   default:
-    assert(0 == "unhandled switch case returned by get_incr_number()");
+    assert(0 == "unhandled case returned by get_incr_number()");
   }
   return true;
 }
@@ -147,7 +147,7 @@ bool get_hunits(hunits *res, unsigned char si, hunits 
prev_value)
     *res = prev_value - v;
     break;
   default:
-    assert(0 == "unhandled switch case returned by get_incr_number()");
+    assert(0 == "unhandled case returned by get_incr_number()");
   }
   return true;
 }
@@ -168,7 +168,7 @@ bool get_number(units *res, unsigned char si, units 
prev_value)
     *res = prev_value - v;
     break;
   default:
-    assert(0 == "unhandled switch case returned by get_incr_number()");
+    assert(0 == "unhandled case returned by get_incr_number()");
   }
   return true;
 }
@@ -189,7 +189,7 @@ bool get_integer(int *res, int prev_value)
     *res = prev_value - int(v);
     break;
   default:
-    assert(0 == "unhandled switch case returned by get_incr_number()");
+    assert(0 == "unhandled case returned by get_incr_number()");
   }
   return true;
 }
@@ -395,7 +395,7 @@ static bool is_valid_expression(units *v, int scaling_unit,
       *v %= v2;
       break;
     default:
-      assert(0 == "unhandled switch case while processing operator");
+      assert(0 == "unhandled case of operator");
     }
   }
   return result;
@@ -644,7 +644,7 @@ static bool is_valid_term(units *v, int scaling_unit,
     *v = scale(*v, sizescale, divisor);
     break;
   default:
-    assert(0 == "unhandled switch case when processing scaling unit");
+    assert(0 == "unhandled case of scaling unit");
   }
   if (do_next)
     tok.next();
diff --git a/src/roff/troff/reg.cpp b/src/roff/troff/reg.cpp
index 3f59ea530..fe4a9e9d1 100644
--- a/src/roff/troff/reg.cpp
+++ b/src/roff/troff/reg.cpp
@@ -80,7 +80,7 @@ static char lowercase_array[] = {
 static const char *number_value_to_ascii(int value, char format, int width)
 {
   static char buf[128];                // must be at least 21
-  switch(format) {
+  switch (format) {
   case '1':
     if (width <= 0)
       return i_to_a(value);
@@ -194,7 +194,7 @@ static const char *number_value_to_ascii(int value, char 
format, int width)
       break;
     }
   default:
-    assert(0);
+    assert(0 == "unhandled case of register format");
     break;
   }
   return buf;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]