changeset 33248:7f73e4805a1f

replace calls to assert with liboctave_panic functions liboctave * lo-error.h (liboctave_panic_impossible, liboctave_panic_if, liboctave_panic_unless): New macros. Replace all calls to assert with panic_if, panic_impossible, or panic_unless as appropriate. Include "lo-error.h" as needed. Don't include <cassert>. Affected files: Array-base.cc, Array-util.cc, Array.h, CSparse.cc, DiagArray2.cc, 1 DiagArray2.h, Sparse.cc, Sparse.h, dim-vector.h, idx-vector.cc, idx-vector.h, CollocWt.cc, Quad.cc, oct-rand.cc, qrp.cc, svd.cc, Sparse-perm-op-defs.h, and oct-sort.cc.
author John W. Eaton <jwe@octave.org>
date Sun, 24 Mar 2024 18:12:34 -0400
parents 55e1412aca35
children 06a308cae32c
files liboctave/array/Array-base.cc liboctave/array/Array-util.cc liboctave/array/Array.h liboctave/array/CSparse.cc liboctave/array/DiagArray2.cc liboctave/array/DiagArray2.h liboctave/array/Sparse.cc liboctave/array/Sparse.h liboctave/array/dim-vector.h liboctave/array/idx-vector.cc liboctave/array/idx-vector.h liboctave/numeric/CollocWt.cc liboctave/numeric/Quad.cc liboctave/numeric/oct-rand.cc liboctave/numeric/qrp.cc liboctave/numeric/svd.cc liboctave/operators/Sparse-perm-op-defs.h liboctave/util/lo-error.h liboctave/util/oct-sort.cc
diffstat 19 files changed, 73 insertions(+), 70 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/array/Array-base.cc	Mon Mar 25 00:43:53 2024 -0400
+++ b/liboctave/array/Array-base.cc	Sun Mar 24 18:12:34 2024 -0400
@@ -35,6 +35,7 @@
 
 #include "Array-util.h"
 #include "Array.h"
+#include "lo-error.h"
 #include "lo-mappers.h"
 #include "oct-locbuf.h"
 
@@ -325,7 +326,7 @@
     : m_n (dv.ndims ()), m_top (0), m_dim (new octave_idx_type [2*m_n]),
       m_stride (m_dim + m_n), m_use_blk (false)
   {
-    assert (m_n == perm.numel ());
+    liboctave_panic_unless (m_n == perm.numel ());
 
     // Get cumulative dimensions.
     OCTAVE_LOCAL_BUFFER (octave_idx_type, cdim, m_n+1);
@@ -535,7 +536,7 @@
     : m_n (ia.numel ()), m_top (0), m_dim (new octave_idx_type [2*m_n]),
       m_cdim (m_dim + m_n), m_idx (new octave::idx_vector [m_n])
   {
-    assert (m_n > 0 && dv.ndims () == std::max (m_n, static_cast<octave_idx_type> (2)));
+    liboctave_panic_unless (m_n > 0 && dv.ndims () == std::max (m_n, static_cast<octave_idx_type> (2)));
 
     m_dim[0] = dv(0);
     m_cdim[0] = 1;
@@ -652,7 +653,7 @@
     : m_cext (nullptr), m_sext (nullptr), m_dext (nullptr), m_n (0)
   {
     int l = ndv.ndims ();
-    assert (odv.ndims () == l);
+    liboctave_panic_unless (odv.ndims () == l);
     octave_idx_type ld = 1;
     int i = 0;
     for (; i < l-1 && ndv(i) == odv(i); i++) ld *= ndv(i);
@@ -1644,7 +1645,7 @@
 Array<T, Alloc>
 Array<T, Alloc>::transpose () const
 {
-  assert (ndims () == 2);
+  liboctave_panic_unless (ndims () == 2);
 
   octave_idx_type nr = dim1 ();
   octave_idx_type nc = dim2 ();
@@ -1687,7 +1688,7 @@
 Array<T, Alloc>
 Array<T, Alloc>::hermitian (T (*fcn) (const T&)) const
 {
-  assert (ndims () == 2);
+  liboctave_panic_unless (ndims () == 2);
 
   if (! fcn)
     fcn = no_op_fcn<T>;
--- a/liboctave/array/Array-util.cc	Mon Mar 25 00:43:53 2024 -0400
+++ b/liboctave/array/Array-util.cc	Sun Mar 24 18:12:34 2024 -0400
@@ -264,7 +264,7 @@
 
   int n = ra_idx.numel ();
 
-  assert (n == dimensions.ndims ());
+  liboctave_panic_unless (n == dimensions.ndims ());
 
   retval.resize (n);
 
@@ -337,7 +337,7 @@
 
   int n = frozen_lengths.ndims ();
 
-  assert (idx_n == n);
+  liboctave_panic_unless (idx_n == n);
 
   for (octave_idx_type i = 0; i < n; i++)
     {
@@ -394,7 +394,7 @@
   for (int i = 0; i < n_dims; i++)
     retval(i) = 0;
 
-  assert (idx > 0 || idx < dims.numel ());
+  liboctave_panic_unless (idx > 0 || idx < dims.numel ());
 
   for (octave_idx_type i = 0; i < idx; i++)
     increment_index (retval, dims);
--- a/liboctave/array/Array.h	Mon Mar 25 00:43:53 2024 -0400
+++ b/liboctave/array/Array.h	Sun Mar 24 18:12:34 2024 -0400
@@ -28,7 +28,6 @@
 
 #include "octave-config.h"
 
-#include <cassert>
 #include <cstddef>
 
 #include <algorithm>
--- a/liboctave/array/CSparse.cc	Mon Mar 25 00:43:53 2024 -0400
+++ b/liboctave/array/CSparse.cc	Sun Mar 24 18:12:34 2024 -0400
@@ -34,6 +34,7 @@
 #include <ostream>
 
 #include "quit.h"
+#include "lo-error.h"
 #include "lo-ieee.h"
 #include "lo-mappers.h"
 #include "f77-fcn.h"
@@ -626,7 +627,7 @@
         retval.xridx (q) = j;
         retval.xdata (q) = conj (data (k));
       }
-  assert (nnz () == retval.xcidx (nr));
+  liboctave_panic_unless (nnz () == retval.xcidx (nr));
   // retval.xcidx[1:nr] holds row entry *end* offsets for rows 0:(nr-1)
   // and retval.xcidx[0:(nr-1)] holds their row entry *start* offsets
 
--- a/liboctave/array/DiagArray2.cc	Mon Mar 25 00:43:53 2024 -0400
+++ b/liboctave/array/DiagArray2.cc	Sun Mar 24 18:12:34 2024 -0400
@@ -27,8 +27,6 @@
 // C++ source files that should have included config.h before including
 // this file.
 
-#include <cassert>
-
 #include <algorithm>
 
 #include "DiagArray2.h"
--- a/liboctave/array/DiagArray2.h	Mon Mar 25 00:43:53 2024 -0400
+++ b/liboctave/array/DiagArray2.h	Sun Mar 24 18:12:34 2024 -0400
@@ -28,7 +28,6 @@
 
 #include "octave-config.h"
 
-#include <cassert>
 #include <cstdlib>
 
 #include "Array.h"
--- a/liboctave/array/Sparse.cc	Mon Mar 25 00:43:53 2024 -0400
+++ b/liboctave/array/Sparse.cc	Sun Mar 24 18:12:34 2024 -0400
@@ -27,7 +27,6 @@
 // C++ source files that should have included config.h before including
 // this file.
 
-#include <cassert>
 #include <cinttypes>
 
 #include <algorithm>
@@ -1139,7 +1138,7 @@
 Sparse<T, Alloc>
 Sparse<T, Alloc>::transpose () const
 {
-  assert (ndims () == 2);
+  liboctave_panic_unless (ndims () == 2);
 
   octave_idx_type nr = rows ();
   octave_idx_type nc = cols ();
@@ -1165,7 +1164,7 @@
         retval.xridx (q) = j;
         retval.xdata (q) = data (k);
       }
-  assert (nnz () == retval.xcidx (nr));
+  liboctave_panic_unless (nnz () == retval.xcidx (nr));
   // retval.xcidx[1:nr] holds row entry *end* offsets for rows 0:(nr-1)
   // and retval.xcidx[0:(nr-1)] holds their row entry *start* offsets
 
@@ -1199,7 +1198,7 @@
 {
   Sparse<T, Alloc> retval;
 
-  assert (ndims () == 2);
+  liboctave_panic_unless (ndims () == 2);
 
   octave_idx_type nr = dim1 ();
   octave_idx_type nc = dim2 ();
@@ -1300,7 +1299,7 @@
 Sparse<T, Alloc>::delete_elements (const octave::idx_vector& idx_i,
                                    const octave::idx_vector& idx_j)
 {
-  assert (ndims () == 2);
+  liboctave_panic_unless (ndims () == 2);
 
   octave_idx_type nr = dim1 ();
   octave_idx_type nc = dim2 ();
@@ -1435,7 +1434,7 @@
 {
   Sparse<T, Alloc> retval;
 
-  assert (ndims () == 2);
+  liboctave_panic_unless (ndims () == 2);
 
   octave_idx_type nr = dim1 ();
   octave_idx_type nc = dim2 ();
@@ -1638,7 +1637,7 @@
 {
   Sparse<T, Alloc> retval;
 
-  assert (ndims () == 2);
+  liboctave_panic_unless (ndims () == 2);
 
   octave_idx_type nr = dim1 ();
   octave_idx_type nc = dim2 ();
@@ -1883,7 +1882,7 @@
 {
   Sparse<T, Alloc> retval;
 
-  assert (ndims () == 2);
+  liboctave_panic_unless (ndims () == 2);
 
   octave_idx_type nr = dim1 ();
   octave_idx_type nc = dim2 ();
@@ -2048,7 +2047,7 @@
 {
   Sparse<T, Alloc> retval;
 
-  assert (ndims () == 2);
+  liboctave_panic_unless (ndims () == 2);
 
   octave_idx_type nr = dim1 ();
   octave_idx_type nc = dim2 ();
@@ -2142,7 +2141,7 @@
                   mx_inline_add (ub - lb, cidx () + lb + 1, rhs.cidx () + 1,
                                  li);
 
-                  assert (nnz () == new_nz);
+                  liboctave_panic_unless (nnz () == new_nz);
                 }
               else
                 {
@@ -2170,7 +2169,7 @@
                   mx_inline_add (nc - ub, cidx () + ub + 1,
                                  tmp.cidx () + ub + 1, new_nz - nz);
 
-                  assert (nnz () == new_nz);
+                  liboctave_panic_unless (nnz () == new_nz);
                 }
             }
           else if (idx_j.is_range () && idx_j.increment () == -1)
@@ -2755,7 +2754,7 @@
         break;
       }
     default:
-      assert (false);
+      liboctave_panic_impossible ();
     }
 
   return retval;
--- a/liboctave/array/Sparse.h	Mon Mar 25 00:43:53 2024 -0400
+++ b/liboctave/array/Sparse.h	Sun Mar 24 18:12:34 2024 -0400
@@ -28,7 +28,6 @@
 
 #include "octave-config.h"
 
-#include <cassert>
 #include <cstddef>
 
 #include <algorithm>
--- a/liboctave/array/dim-vector.h	Mon Mar 25 00:43:53 2024 -0400
+++ b/liboctave/array/dim-vector.h	Sun Mar 24 18:12:34 2024 -0400
@@ -30,8 +30,6 @@
 
 #include "octave-config.h"
 
-#include <cassert>
-
 #include <algorithm>
 #include <initializer_list>
 #include <string>
--- a/liboctave/array/idx-vector.cc	Mon Mar 25 00:43:53 2024 -0400
+++ b/liboctave/array/idx-vector.cc	Sun Mar 24 18:12:34 2024 -0400
@@ -1030,7 +1030,7 @@
 
   idx_vector_rep *r = dynamic_cast<idx_vector_rep *> (m_rep);
 
-  assert (r != nullptr);
+  liboctave_panic_unless (r != nullptr);
 
   return r->get_data ();
 }
@@ -1088,7 +1088,7 @@
       break;
 
     default:
-      assert (false);
+      liboctave_panic_impossible ();
       break;
     }
 }
@@ -1156,7 +1156,7 @@
 idx_vector
 idx_vector::inverse_permutation (octave_idx_type n) const
 {
-  assert (n == length (n));
+  liboctave_panic_unless (n == length (n));
 
   idx_vector retval;
 
@@ -1252,7 +1252,7 @@
       break;
 
     default:
-      assert (false);
+      liboctave_panic_impossible ();
       break;
     }
 }
--- a/liboctave/array/idx-vector.h	Mon Mar 25 00:43:53 2024 -0400
+++ b/liboctave/array/idx-vector.h	Sun Mar 24 18:12:34 2024 -0400
@@ -28,7 +28,6 @@
 
 #include "octave-config.h"
 
-#include <cassert>
 #include <cstring>
 
 #include <algorithm>
@@ -37,6 +36,7 @@
 
 #include "Array-fwd.h"
 #include "dim-vector.h"
+#include "lo-error.h"
 #include "oct-inttypes.h"
 #include "oct-refcount.h"
 #include "Sparse-fwd.h"
@@ -638,7 +638,7 @@
         break;
 
       default:
-        assert (false);
+        liboctave_panic_impossible ();
         break;
       }
 
@@ -710,7 +710,7 @@
         break;
 
       default:
-        assert (false);
+        liboctave_panic_impossible ();
         break;
       }
 
@@ -782,7 +782,7 @@
         break;
 
       default:
-        assert (false);
+        liboctave_panic_impossible ();
         break;
       }
 
@@ -848,7 +848,7 @@
         break;
 
       default:
-        assert (false);
+        liboctave_panic_impossible ();
         break;
       }
 
@@ -934,7 +934,7 @@
         break;
 
       default:
-        assert (false);
+        liboctave_panic_impossible ();
         break;
       }
 
--- a/liboctave/numeric/CollocWt.cc	Mon Mar 25 00:43:53 2024 -0400
+++ b/liboctave/numeric/CollocWt.cc	Sun Mar 24 18:12:34 2024 -0400
@@ -27,7 +27,6 @@
 #  include "config.h"
 #endif
 
-#include <cassert>
 #include <cmath>
 
 #include <limits>
@@ -152,12 +151,12 @@
        double alpha, double beta, double *dif1, double *dif2,
        double *dif3, double *root)
 {
-  assert (n0 == 0 || n0 == 1);
-  assert (n1 == 0 || n1 == 1);
+  liboctave_panic_unless (n0 == 0 || n0 == 1);
+  liboctave_panic_unless (n1 == 0 || n1 == 1);
 
   octave_idx_type nt = n + n0 + n1;
 
-  assert (nt >= 1);
+  liboctave_panic_unless (nt >= 1);
 
   // -- first evaluation of coefficients in recursion formulas.
   // -- recursion coefficients are stored in dif1 and dif2.
@@ -314,17 +313,17 @@
        octave_idx_type i, octave_idx_type id, double *dif1,
        double *dif2, double *dif3, double *root, double *vect)
 {
-  assert (n0 == 0 || n0 == 1);
-  assert (n1 == 0 || n1 == 1);
+  liboctave_panic_unless (n0 == 0 || n0 == 1);
+  liboctave_panic_unless (n1 == 0 || n1 == 1);
 
   octave_idx_type nt = n + n0 + n1;
 
-  assert (nt >= 1);
+  liboctave_panic_unless (nt >= 1);
 
-  assert (id == 1 || id == 2 || id == 3);
+  liboctave_panic_unless (id == 1 || id == 2 || id == 3);
 
   if (id != 3)
-    assert (i >= 0 && i < nt);
+    liboctave_panic_unless (i >= 0 && i < nt);
 
   // Evaluate discretization matrices and Gaussian quadrature weights.
   // Quadrature weights are normalized to sum to one.
--- a/liboctave/numeric/Quad.cc	Mon Mar 25 00:43:53 2024 -0400
+++ b/liboctave/numeric/Quad.cc	Sun Mar 24 18:12:34 2024 -0400
@@ -27,8 +27,6 @@
 #  include "config.h"
 #endif
 
-#include <cassert>
-
 #include "Array.h"
 #include "Quad.h"
 #include "f77-fcn.h"
@@ -172,7 +170,7 @@
       break;
 
     default:
-      assert (0);
+      liboctave_panic_impossible ();
       break;
     }
 
@@ -289,7 +287,7 @@
       break;
 
     default:
-      assert (0);
+      liboctave_panic_impossible ();
       break;
     }
 
--- a/liboctave/numeric/oct-rand.cc	Mon Mar 25 00:43:53 2024 -0400
+++ b/liboctave/numeric/oct-rand.cc	Sun Mar 24 18:12:34 2024 -0400
@@ -27,7 +27,6 @@
 #  include "config.h"
 #endif
 
-#include <cassert>
 #include <cstdint>
 
 #include <limits>
@@ -98,7 +97,7 @@
 static int32_t
 force_to_fit_range (int32_t i, int32_t lo, int32_t hi)
 {
-  assert (hi > lo && lo >= 0);
+  liboctave_panic_unless (hi > lo && lo >= 0);
 
   i = (i > 0 ? i : -i);
 
--- a/liboctave/numeric/qrp.cc	Mon Mar 25 00:43:53 2024 -0400
+++ b/liboctave/numeric/qrp.cc	Sun Mar 24 18:12:34 2024 -0400
@@ -27,8 +27,6 @@
 #  include "config.h"
 #endif
 
-#include <cassert>
-
 #include <algorithm>
 
 #include "Array.h"
@@ -54,7 +52,7 @@
 void
 qrp<Matrix>::init (const Matrix& a, type qr_type)
 {
-  assert (qr_type != qr<Matrix>::raw);
+  liboctave_panic_if (qr_type == qr<Matrix>::raw);
 
   F77_INT m = to_f77_int (a.rows ());
   F77_INT n = to_f77_int (a.cols ());
@@ -125,7 +123,7 @@
 void
 qrp<FloatMatrix>::init (const FloatMatrix& a, type qr_type)
 {
-  assert (qr_type != qr<FloatMatrix>::raw);
+  liboctave_panic_if (qr_type == qr<FloatMatrix>::raw);
 
   F77_INT m = to_f77_int (a.rows ());
   F77_INT n = to_f77_int (a.cols ());
@@ -196,7 +194,7 @@
 void
 qrp<ComplexMatrix>::init (const ComplexMatrix& a, type qr_type)
 {
-  assert (qr_type != qr<ComplexMatrix>::raw);
+  liboctave_panic_if (qr_type == qr<ComplexMatrix>::raw);
 
   F77_INT m = to_f77_int (a.rows ());
   F77_INT n = to_f77_int (a.cols ());
@@ -275,7 +273,7 @@
 void
 qrp<FloatComplexMatrix>::init (const FloatComplexMatrix& a, type qr_type)
 {
-  assert (qr_type != qr<FloatComplexMatrix>::raw);
+  liboctave_panic_if (qr_type == qr<FloatComplexMatrix>::raw);
 
   F77_INT m = to_f77_int (a.rows ());
   F77_INT n = to_f77_int (a.cols ());
--- a/liboctave/numeric/svd.cc	Mon Mar 25 00:43:53 2024 -0400
+++ b/liboctave/numeric/svd.cc	Sun Mar 24 18:12:34 2024 -0400
@@ -27,8 +27,6 @@
 #  include "config.h"
 #endif
 
-#include <cassert>
-
 #include <algorithm>
 #include <unordered_map>
 
@@ -774,7 +772,7 @@
            work, lwork, info);
   else if (m_driver == svd::Driver::GESDD)
     {
-      assert (jobu == jobv);
+      liboctave_panic_unless (jobu == jobv);
       char jobz = jobu;
 
       std::vector<F77_INT> iwork (8 * std::min (m, n));
--- a/liboctave/operators/Sparse-perm-op-defs.h	Mon Mar 25 00:43:53 2024 -0400
+++ b/liboctave/operators/Sparse-perm-op-defs.h	Sun Mar 24 18:12:34 2024 -0400
@@ -112,7 +112,7 @@
           r.xdata (k) = a.data (k_src);
         }
     }
-  assert (k_src == nent);
+  liboctave_panic_unless (k_src == nent);
 
   return r;
 }
@@ -132,7 +132,7 @@
       const octave_idx_type j_src = pcol[j];
       r.xcidx (j+1) = r.xcidx (j) + (a.cidx (j_src+1) - a.cidx (j_src));
     }
-  assert (r.xcidx (nc) == nent);
+  liboctave_panic_unless (r.xcidx (nc) == nent);
 
   octave_idx_type k = 0;
   for (octave_idx_type j = 0; j < nc; ++j)
@@ -147,7 +147,7 @@
           r.xdata (k) = a.data (k_src);
         }
     }
-  assert (k == nent);
+  liboctave_panic_unless (k == nent);
 
   return r;
 }
--- a/liboctave/util/lo-error.h	Mon Mar 25 00:43:53 2024 -0400
+++ b/liboctave/util/lo-error.h	Sun Mar 24 18:12:34 2024 -0400
@@ -81,6 +81,24 @@
 extern OCTAVE_API void
 set_liboctave_warning_with_id_handler (liboctave_warning_with_id_handler f);
 
+// To allow the __FILE__ and __LINE__ macros to work as expected, the
+// liboctave_panic_impossible, liboctave_panic_if, and
+// liboctave_panic_unless symbols must be defined as macros.
+
+#define liboctave_panic_impossible() (*current_liboctave_error_handler) ("impossible state reached in file '%s' at line %d", __FILE__, __LINE__)
+
+#if defined (NDEBUG)
+#  define liboctave_panic_if(cond)
+#else
+#  define liboctave_panic_if(cond) do { if (cond) liboctave_panic_impossible (); } while (0)
+#endif
+
+#if defined (NDEBUG)
+#  define liboctave_panic_unless(cond)
+#else
+#  define liboctave_panic_unless(cond) liboctave_panic_if (! (cond))
+#endif
+
 #if defined (__cplusplus)
 }
 #endif
--- a/liboctave/util/oct-sort.cc	Mon Mar 25 00:43:53 2024 -0400
+++ b/liboctave/util/oct-sort.cc	Sun Mar 24 18:12:34 2024 -0400
@@ -107,7 +107,6 @@
 // C++ source files that should have included config.h before including
 // this file.
 
-#include <cassert>
 #include <algorithm>
 #include <cstring>
 #include <stack>
@@ -1437,7 +1436,7 @@
               n = force;
             }
           /* Push run onto m_pending-runs stack, and maybe merge. */
-          assert (m_ms->m_n < MAX_MERGE_PENDING);
+          liboctave_panic_unless (m_ms->m_n < MAX_MERGE_PENDING);
           m_ms->m_pending[m_ms->m_n].m_base = lo;
           m_ms->m_pending[m_ms->m_n].m_len = n;
           m_ms->m_n++;
@@ -1497,7 +1496,7 @@
               n = force;
             }
           /* Push run onto m_pending-runs stack, and maybe merge. */
-          assert (m_ms->m_n < MAX_MERGE_PENDING);
+          liboctave_panic_unless (m_ms->m_n < MAX_MERGE_PENDING);
           m_ms->m_pending[m_ms->m_n].m_base = lo;
           m_ms->m_pending[m_ms->m_n].m_len = n;
           m_ms->m_n++;
@@ -1630,7 +1629,7 @@
       octave_idx_type ofs = runs.top ().ofs;
       octave_idx_type nel = runs.top ().nel;
       runs.pop ();
-      assert (nel > 1);
+      liboctave_panic_unless (nel > 1);
 
       T *lbuf = buf + ofs;
       const T *ldata = data + rows*col;
@@ -1705,7 +1704,7 @@
       if (lo < lastrow)
         {
           // Not the final column.
-          assert (n > 1);
+          liboctave_panic_unless (n > 1);
           const T *hi = lo + n;
           const T *lst = lo;
           for (lo++; lo < hi; lo++)