changeset 10370:9c4daf174387

implement IDs for common liboctave exceptions
author Jaroslav Hajek <highegg@gmail.com>
date Sun, 28 Feb 2010 08:19:49 +0100
parents 3516a245d607
children dc8637fd7a76
files libcruft/ChangeLog libcruft/misc/lo-error.c libcruft/misc/lo-error.h liboctave/Array-util.cc liboctave/Array-util.h liboctave/Array.cc liboctave/ChangeLog liboctave/idx-vector.cc src/octave.cc src/ov-class.cc src/ov-struct.cc
diffstat 11 files changed, 145 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/libcruft/ChangeLog	Sat Feb 27 23:04:09 2010 -0500
+++ b/libcruft/ChangeLog	Sun Feb 28 08:19:49 2010 +0100
@@ -1,3 +1,11 @@
+2010-02-28  Jaroslav Hajek  <highegg@gmail.com>
+
+	* misc/lo-error.c (liboctave_fatal_with_id,
+	set_liboctave_error_with_id_handler): New functions.
+	(current_liboctave_error_with_id_handler): New variable.
+	* misc/lo-error.h: Declare them.
+	(liboctave_error_with_id_handler): New typedef.
+
 2010-02-21  Michael Goffioul  <michael.goffioul@gmail.com>
 
 	* misc/quit.h: Define WIN32_LEAN_AND_MEAN and don't #undef min/max.
--- a/libcruft/misc/lo-error.c	Sat Feb 27 23:04:09 2010 -0500
+++ b/libcruft/misc/lo-error.c	Sun Feb 28 08:19:49 2010 +0100
@@ -37,6 +37,10 @@
 liboctave_error_handler current_liboctave_error_handler
   = liboctave_fatal;
 
+/* Pointer to the current error_with_id handling function. */
+liboctave_error_with_id_handler current_liboctave_error_with_id_handler
+  = liboctave_fatal_with_id;
+
 /* Pointer to the current warning handler. */
 liboctave_warning_handler current_liboctave_warning_handler
   = liboctave_warning;
@@ -66,6 +70,15 @@
 }
 
 void
+set_liboctave_error_with_id_handler (liboctave_error_with_id_handler f)
+{
+  if (f)
+    current_liboctave_error_with_id_handler = f;
+  else
+    current_liboctave_error_with_id_handler = liboctave_fatal_with_id;
+}
+
+void
 set_liboctave_warning_handler (liboctave_warning_handler f)
 {
   if (f)
@@ -95,6 +108,17 @@
 }
 
 void
+liboctave_fatal_with_id (const char *id, const char *fmt, ...)
+{
+  va_list args;
+  va_start (args, fmt);
+  verror ("fatal", fmt, args);
+  va_end (args);
+
+  exit (1);
+}
+
+void
 liboctave_warning (const char *fmt, ...)
 {
   va_list args;
--- a/libcruft/misc/lo-error.h	Sat Feb 27 23:04:09 2010 -0500
+++ b/libcruft/misc/lo-error.h	Sun Feb 28 08:19:49 2010 +0100
@@ -29,12 +29,16 @@
 
 extern void liboctave_fatal (const char *fmt, ...) GCC_ATTR_NORETURN;
 
+extern void liboctave_fatal_with_id (const char *id, const char *fmt, ...) GCC_ATTR_NORETURN;
+
 extern void liboctave_warning (const char *fmt, ...);
 
 extern void liboctave_warning_with_id (const char *id, const char *fmt, ...);
 
 typedef void (*liboctave_error_handler) (const char *, ...);
 
+typedef void (*liboctave_error_with_id_handler) (const char *, const char *, ...);
+
 typedef void (*liboctave_warning_handler) (const char *, ...);
 
 typedef void (*liboctave_warning_with_id_handler) (const char *, const char *, ...);
@@ -43,12 +47,16 @@
    them among all the liboctave classes. */
 CRUFT_API extern liboctave_error_handler current_liboctave_error_handler;
 
+CRUFT_API extern liboctave_error_with_id_handler current_liboctave_error_with_id_handler;
+
 CRUFT_API extern liboctave_warning_handler current_liboctave_warning_handler;
 
 CRUFT_API extern liboctave_warning_with_id_handler current_liboctave_warning_with_id_handler;
 
 CRUFT_API extern void set_liboctave_error_handler (liboctave_error_handler f);
 
+CRUFT_API extern void set_liboctave_error_with_id_handler (liboctave_error_with_id_handler f);
+
 CRUFT_API extern void set_liboctave_warning_handler (liboctave_warning_handler f);
 
 CRUFT_API extern void set_liboctave_warning_with_id_handler (liboctave_warning_with_id_handler f);
--- a/liboctave/Array-util.cc	Sat Feb 27 23:04:09 2010 -0500
+++ b/liboctave/Array-util.cc	Sun Feb 28 08:19:49 2010 +0100
@@ -617,6 +617,12 @@
   return pva->pidx > pvb->pidx;
 }
 
+const char *error_id_nonconformant_args = "Octave:nonconformant-args";
+
+const char *error_id_index_out_of_bounds = "Octave:index-out-of-bounds";
+
+const char *error_id_invalid_index = "Octave:invalid-index";
+
 void
 gripe_nan_to_logical_conversion (void)
 {
@@ -626,8 +632,9 @@
 void
 gripe_nonconformant (const char *op, int op1_len, int op2_len)
 {
-  (*current_liboctave_error_handler)
-    ("%s: nonconformant arguments (op1 len: %d, op2 len: %d)",
+  const char *err_id = error_id_nonconformant_args;
+  (*current_liboctave_error_with_id_handler)
+    (err_id, "%s: nonconformant arguments (op1 len: %d, op2 len: %d)",
      op, op1_len, op2_len);
 }
 
@@ -635,8 +642,9 @@
 gripe_nonconformant (const char *op, int op1_nr, int op1_nc,
                      int op2_nr, int op2_nc)
 {
-  (*current_liboctave_error_handler)
-    ("%s: nonconformant arguments (op1 is %dx%d, op2 is %dx%d)",
+  const char *err_id = error_id_nonconformant_args;
+  (*current_liboctave_error_with_id_handler)
+    (err_id, "%s: nonconformant arguments (op1 is %dx%d, op2 is %dx%d)",
      op, op1_nr, op1_nc, op2_nr, op2_nc);
 }
 
@@ -644,32 +652,34 @@
 gripe_nonconformant (const char *op, const dim_vector& op1_dims,
                      const dim_vector& op2_dims)
 {
+  const char *err_id = error_id_nonconformant_args;
   std::string op1_dims_str = op1_dims.str ();
   std::string op2_dims_str = op2_dims.str ();
 
-  (*current_liboctave_error_handler)
-    ("%s: nonconformant arguments (op1 is %s, op2 is %s)",
+  (*current_liboctave_error_with_id_handler)
+    (err_id, "%s: nonconformant arguments (op1 is %s, op2 is %s)",
      op, op1_dims_str.c_str (), op2_dims_str.c_str ());
 }
 
 void gripe_index_out_of_range (int nd, int dim, 
                                octave_idx_type idx, octave_idx_type ext)
 {
+  const char *err_id = error_id_index_out_of_bounds;
   switch (nd)
     {
     case 1:
-      (*current_liboctave_error_handler)
-        ("A(I): index out of bounds; value %d out of bound %d",
+      (*current_liboctave_error_with_id_handler)
+        (err_id, "A(I): index out of bounds; value %d out of bound %d",
          idx, ext);
       break;
     case 2:
-      (*current_liboctave_error_handler)
-        ("A(I,J): %s index out of bounds; value %d out of bound %d",
+      (*current_liboctave_error_with_id_handler)
+        (err_id, "A(I,J): %s index out of bounds; value %d out of bound %d",
          (dim == 1) ? "row" : "column", idx, ext);
       break;
     default:
-      (*current_liboctave_error_handler)
-        ("A(I,J,...): index to dimension %d out of bounds; value %d out of bound %d",
+      (*current_liboctave_error_with_id_handler)
+        (err_id, "A(I,J,...): index to dimension %d out of bounds; value %d out of bound %d",
          dim, idx, ext);
       break;
     }
@@ -678,7 +688,16 @@
 void gripe_del_index_out_of_range (bool is1d, octave_idx_type idx, 
                                    octave_idx_type ext)
 {
-  (*current_liboctave_error_handler)
-    ("A(%s) = []: index out of bounds; value %d out of bound %d",
+  const char *err_id = error_id_index_out_of_bounds;
+  (*current_liboctave_error_with_id_handler)
+    (err_id, "A(%s) = []: index out of bounds; value %d out of bound %d",
      is1d ? "I" : "..,I,..", idx, ext);
 }
+
+void gripe_invalid_index (void)
+{
+  const char *err_id = error_id_invalid_index;
+  (*current_liboctave_error_with_id_handler)
+    (err_id, "subscript indices must be either positive integers or logicals.");
+}
+
--- a/liboctave/Array-util.h	Sat Feb 27 23:04:09 2010 -0500
+++ b/liboctave/Array-util.h	Sun Feb 28 08:19:49 2010 +0100
@@ -91,6 +91,12 @@
 
 extern int OCTAVE_API permute_vector_compare (const void *a, const void *b);
 
+extern OCTAVE_API const char *error_id_nonconformant_args;
+
+extern OCTAVE_API const char *error_id_index_out_of_bounds;
+
+extern OCTAVE_API const char *error_id_invalid_index;
+
 extern void OCTAVE_API gripe_nan_to_logical_conversion (void);
 
 extern void OCTAVE_API gripe_nonconformant (const char *op, int op1_len, int op2_len);
@@ -108,4 +114,6 @@
 extern void OCTAVE_API gripe_del_index_out_of_range (bool is1d, octave_idx_type iext, 
                                                      octave_idx_type ext);
 
+extern void OCTAVE_API gripe_invalid_index (void);
+
 #endif
--- a/liboctave/Array.cc	Sat Feb 27 23:04:09 2010 -0500
+++ b/liboctave/Array.cc	Sun Feb 28 08:19:49 2010 +0100
@@ -209,7 +209,9 @@
 T& 
 Array<T>::checkelem (octave_idx_type n)
 {
-  if (n < 0 || n >= slice_len)
+  if (n < 0)
+    gripe_invalid_index ();
+  if (n >= slice_len)
     gripe_index_out_of_range (1, 1, n+1, slice_len);
 
   return elem (n);
@@ -219,9 +221,11 @@
 T& 
 Array<T>::checkelem (octave_idx_type i, octave_idx_type j)
 {
-  if (i < 0 || i >= dim1 ())
+  if (i < 0 || j < 0)
+    gripe_invalid_index ();
+  if (i >= dim1 ())
     gripe_index_out_of_range (2, 1, i+1, dim1 ());
-  if (j < 0 || j >= dimensions.numel (1))
+  if (j >= dimensions.numel (1))
     gripe_index_out_of_range (2, 2, j+1, dimensions.numel (1));
 
   return elem (i, j);
@@ -231,11 +235,13 @@
 T& 
 Array<T>::checkelem (octave_idx_type i, octave_idx_type j, octave_idx_type k)
 {
-  if (i < 0 || i >= dim1 ())
+  if (i < 0 || j < 0 || k < 0)
+    gripe_invalid_index ();
+  if (i >= dim1 ())
     gripe_index_out_of_range (3, 1, i+1, dim1 ());
-  if (j < 0 || j >= dim2 ())
+  if (j >= dim2 ())
     gripe_index_out_of_range (3, 2, j+1, dim2 ());
-  if (k < 0 || k >= dimensions.numel (2))
+  if (k >= dimensions.numel (2))
     gripe_index_out_of_range (3, 3, k+1, dimensions.numel (2));
 
   return elem (i, j, k);
@@ -248,8 +254,12 @@
   int nd = ra_idx.length ();
   const dim_vector dv = dimensions.redim (nd);
   for (int d = 0; d < nd; d++)
-    if (ra_idx(d) < 0 || ra_idx(d) >= dv(d))
-      gripe_index_out_of_range (nd, d+1, ra_idx(d)+1, dv(d));
+    {
+      if (ra_idx(d) < 0)
+        gripe_invalid_index ();
+      if (ra_idx(d) >= dv(d))
+        gripe_index_out_of_range (nd, d+1, ra_idx(d)+1, dv(d));
+    }
 
   return elem (ra_idx);
 }
@@ -258,7 +268,9 @@
 typename Array<T>::crefT
 Array<T>::checkelem (octave_idx_type n) const
 {
-  if (n < 0 || n >= slice_len)
+  if (n < 0)
+    gripe_invalid_index ();
+  if (n >= slice_len)
     gripe_index_out_of_range (1, 1, n+1, slice_len);
 
   return elem (n);
@@ -268,9 +280,11 @@
 typename Array<T>::crefT
 Array<T>::checkelem (octave_idx_type i, octave_idx_type j) const
 {
-  if (i < 0 || i >= dim1 ())
+  if (i < 0 || j < 0)
+    gripe_invalid_index ();
+  if (i >= dim1 ())
     gripe_index_out_of_range (2, 1, i+1, dim1 ());
-  if (j < 0 || j >= dimensions.numel (1))
+  if (j >= dimensions.numel (1))
     gripe_index_out_of_range (2, 2, j+1, dimensions.numel (1));
 
   return elem (i, j);
@@ -280,11 +294,13 @@
 typename Array<T>::crefT
 Array<T>::checkelem (octave_idx_type i, octave_idx_type j, octave_idx_type k) const
 {
-  if (i < 0 || i >= dim1 ())
+  if (i < 0 || j < 0 || k < 0)
+    gripe_invalid_index ();
+  if (i >= dim1 ())
     gripe_index_out_of_range (3, 1, i+1, dim1 ());
-  if (j < 0 || j >= dim2 ())
+  if (j >= dim2 ())
     gripe_index_out_of_range (3, 2, j+1, dim2 ());
-  if (k < 0 || k >= dimensions.numel (2))
+  if (k >= dimensions.numel (2))
     gripe_index_out_of_range (3, 3, k+1, dimensions.numel (2));
 
   return elem (i, j, k);
@@ -297,8 +313,12 @@
   int nd = ra_idx.length ();
   const dim_vector dv = dimensions.redim (nd);
   for (int d = 0; d < nd; d++)
-    if (ra_idx(d) < 0 || ra_idx(d) >= dv(d))
-      gripe_index_out_of_range (nd, d+1, ra_idx(d)+1, dv(d));
+    {
+      if (ra_idx(d) < 0)
+        gripe_invalid_index ();
+      if (ra_idx(d) >= dv(d))
+        gripe_index_out_of_range (nd, d+1, ra_idx(d)+1, dv(d));
+    }
 
   return elem (ra_idx);
 }
--- a/liboctave/ChangeLog	Sat Feb 27 23:04:09 2010 -0500
+++ b/liboctave/ChangeLog	Sun Feb 28 08:19:49 2010 +0100
@@ -1,3 +1,14 @@
+2010-02-28  Jaroslav Hajek  <highegg@gmail.com>
+
+	* Array-util.cc (gripe_index_out_of_range): Throw errors with id.
+	(gripe_nonconformant): Likewise.
+	(error_id_index_out_of_bounds, error_id_invalid_index,
+	error_id_nonconformant_args): New variables.
+	(gripe_invalid_index): New function.
+	* Array-util.h: Declare them.
+	* Array.cc (Array<T>::checkelem): Throw invalid-index when negative
+	indices are encountered.
+
 2010-02-27  John W. Eaton  <jwe@octave.org>
 
 	* Sparse.cc (Sparse<T>::diag): Handle case of diag (szv) when szv
--- a/liboctave/idx-vector.cc	Sat Feb 27 23:04:09 2010 -0500
+++ b/liboctave/idx-vector.cc	Sun Feb 28 08:19:49 2010 +0100
@@ -34,6 +34,7 @@
 
 #include "idx-vector.h"
 #include "Array.h"
+#include "Array-util.h"
 #include "Sparse.h"
 #include "Range.h"
 
@@ -42,13 +43,6 @@
 #include "lo-mappers.h"
 
 static void
-gripe_invalid_index (void)
-{
-  (*current_liboctave_error_handler)
-    ("subscript indices must be either positive integers or logicals.");
-}
-
-static void
 gripe_invalid_range (void)
 {
   (*current_liboctave_error_handler)
--- a/src/octave.cc	Sat Feb 27 23:04:09 2010 -0500
+++ b/src/octave.cc	Sun Feb 28 08:19:49 2010 +0100
@@ -535,9 +535,21 @@
 }
 
 static void
+lo_error_with_id_handler (const char *id, const char *fmt, ...)
+{
+  va_list args;
+  va_start (args, fmt);
+  verror_with_id_cfn (id, fmt, args);
+  va_end (args);
+
+  octave_throw_execution_exception ();
+}
+
+static void
 initialize_error_handlers ()
 {
   set_liboctave_error_handler (lo_error_handler);
+  set_liboctave_error_with_id_handler (lo_error_with_id_handler);
   set_liboctave_warning_handler (warning);
   set_liboctave_warning_with_id_handler (warning_with_id);
 }
--- a/src/ov-class.cc	Sat Feb 27 23:04:09 2010 -0500
+++ b/src/ov-class.cc	Sun Feb 28 08:19:49 2010 +0100
@@ -130,7 +130,7 @@
 }
 
 static void
-gripe_invalid_index (void)
+gripe_invalid_index1 (void)
 {
   error ("invalid index for class");
 }
@@ -275,7 +275,7 @@
         error ("class has no member `%s'", nm.c_str ());
     }
   else
-    gripe_invalid_index ();
+    gripe_invalid_index1 ();
 
   return retval;
 }
@@ -469,7 +469,7 @@
           if (type.length () == 1 && type[0] == '(')
             retval(0) = octave_value (map.index (idx.front ()), class_name ());
           else
-            gripe_invalid_index ();
+            gripe_invalid_index1 ();
         }
     }
 
--- a/src/ov-struct.cc	Sat Feb 27 23:04:09 2010 -0500
+++ b/src/ov-struct.cc	Sun Feb 28 08:19:49 2010 +0100
@@ -74,7 +74,7 @@
 
 #if 0
 static void
-gripe_invalid_index (void)
+gripe_invalid_index1 (void)
 {
   error ("invalid index for structure array");
 }