changeset 33380:c9517a0c9cc2

rely on compiler warnings for unhandled enum switch cases in idx_vector class * idx-vector.h, idx-vector.cc: Don't use default cases in switch statements that use enum values so that compilers may warn about unhandled enum values. Use error to report enum values that are not accepted by the switch, use error with a meaningful message instead of panic_impossible.
author John W. Eaton <jwe@octave.org>
date Fri, 12 Apr 2024 10:47:48 -0400
parents 7e7d52c1e03a
children 6f7fe3f85253
files liboctave/array/idx-vector.cc liboctave/array/idx-vector.h
diffstat 2 files changed, 54 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/array/idx-vector.cc	Fri Apr 12 10:21:16 2024 -0400
+++ b/liboctave/array/idx-vector.cc	Fri Apr 12 10:47:48 2024 -0400
@@ -1042,6 +1042,10 @@
 
   switch (m_rep->idx_class ())
     {
+    case class_invalid:
+      (*current_liboctave_error_handler) ("unexpected: invalid index");
+      break;
+
     case class_colon:
       (*current_liboctave_error_handler) ("colon not allowed");
       break;
@@ -1087,9 +1091,9 @@
       }
       break;
 
-    default:
-      liboctave_panic_impossible ();
-      break;
+      // We should have handled all possible enum values above.  Rely on
+      // compiler diagnostics to warn if we haven't.  For example, GCC's
+      // -Wswitch option, enabled by -Wall, will provide a warning.
     }
 }
 
@@ -1220,6 +1224,10 @@
   iclass = idx_class ();
   switch (iclass)
     {
+    case class_invalid:
+      (*current_liboctave_error_handler) ("unexpected: invalid index");
+      break;
+
     case class_colon:
       break;
 
@@ -1251,9 +1259,9 @@
       }
       break;
 
-    default:
-      liboctave_panic_impossible ();
-      break;
+      // We should have handled all possible enum values above.  Rely on
+      // compiler diagnostics to warn if we haven't.  For example, GCC's
+      // -Wswitch option, enabled by -Wall, will provide a warning.
     }
 }
 
--- a/liboctave/array/idx-vector.h	Fri Apr 12 10:21:16 2024 -0400
+++ b/liboctave/array/idx-vector.h	Fri Apr 12 10:47:48 2024 -0400
@@ -587,6 +587,10 @@
 
     switch (m_rep->idx_class ())
       {
+      case class_invalid:
+        (*current_liboctave_error_handler) ("unexpected: invalid index");
+        break;
+
       case class_colon:
         std::copy_n (src, len, dest);
         break;
@@ -637,9 +641,10 @@
         }
         break;
 
-      default:
-        liboctave_panic_impossible ();
-        break;
+        // We should have handled all possible enum values above.  Rely
+        // on compiler diagnostics to warn if we haven't.  For example,
+        // GCC's -Wswitch option, enabled by -Wall, will provide a
+        // warning.
       }
 
     return len;
@@ -661,6 +666,10 @@
 
     switch (m_rep->idx_class ())
       {
+      case class_invalid:
+        (*current_liboctave_error_handler) ("unexpected: invalid index");
+        break;
+
       case class_colon:
         std::copy_n (src, len, dest);
         break;
@@ -709,9 +718,10 @@
         }
         break;
 
-      default:
-        liboctave_panic_impossible ();
-        break;
+        // We should have handled all possible enum values above.  Rely
+        // on compiler diagnostics to warn if we haven't.  For example,
+        // GCC's -Wswitch option, enabled by -Wall, will provide a
+        // warning.
       }
 
     return len;
@@ -733,6 +743,10 @@
 
     switch (m_rep->idx_class ())
       {
+      case class_invalid:
+        (*current_liboctave_error_handler) ("unexpected: invalid index");
+        break;
+
       case class_colon:
         std::fill_n (dest, len, val);
         break;
@@ -781,9 +795,10 @@
         }
         break;
 
-      default:
-        liboctave_panic_impossible ();
-        break;
+        // We should have handled all possible enum values above.  Rely
+        // on compiler diagnostics to warn if we haven't.  For example,
+        // GCC's -Wswitch option, enabled by -Wall, will provide a
+        // warning.
       }
 
     return len;
@@ -803,6 +818,10 @@
 
     switch (m_rep->idx_class ())
       {
+      case class_invalid:
+        (*current_liboctave_error_handler) ("unexpected: invalid index");
+        break;
+
       case class_colon:
         for (octave_idx_type i = 0; i < len; i++) body (i);
         break;
@@ -847,9 +866,10 @@
         }
         break;
 
-      default:
-        liboctave_panic_impossible ();
-        break;
+        // We should have handled all possible enum values above.  Rely
+        // on compiler diagnostics to warn if we haven't.  For example,
+        // GCC's -Wswitch option, enabled by -Wall, will provide a
+        // warning.
       }
 
   }
@@ -871,6 +891,10 @@
 
     switch (m_rep->idx_class ())
       {
+      case class_invalid:
+        (*current_liboctave_error_handler) ("unexpected: invalid index");
+        break;
+
       case class_colon:
         {
           octave_idx_type i;
@@ -933,9 +957,10 @@
         }
         break;
 
-      default:
-        liboctave_panic_impossible ();
-        break;
+        // We should have handled all possible enum values above.  Rely
+        // on compiler diagnostics to warn if we haven't.  For example,
+        // GCC's -Wswitch option, enabled by -Wall, will provide a
+        // warning.
       }
 
     return ret;