changeset 20652:7a8096f8df5d

more cleanup of indexing exceptions * data.cc (index_error): New function. (Fnth_element, F__accumarray_sum__, __accumdim_sum__): Use it. * lo-array-gripes.h, lo-array-gripes.cc (index_exception::message): Rename from index_exception::err. Return std::string instead of const char *. Change all uses. (index_exception::expression): Rename from index_exception::access. (index_exception::index_exception, invalid_index::invalid_index, out_of_range::out_of_range): Accept const string& instead of const char *. Change all uses. (invalid_index::details): Rename from invalid_index::explain. Return std::string instead of const char *. Change all uses. (invalid_index::err_idx): Rename from invalid_index::id. Return std::string instead of const char *. Change all uses. (gripe_invalid_index): Accept const std::string& argument instead of const char *. (index_exception::~index_exception): Don't declare with "throw ()". (index_exception::details): Rename from index_exception::explain. Return std::string instead of const char *. Change all uses. (index_exception::err_idx): Rename from index_exception::id. Return std::string instead of const char *. Change all uses. (index_exception::idx): Return std::string instead of const char *. Change all uses. (index_exception::err_id): Rename from index_exception::id. (index_exception::msg): Delete member variable. (index_expression::expression): Rename from index_expression::access. Change all uses.
author John W. Eaton <jwe@octave.org>
date Wed, 21 Oct 2015 23:43:46 -0400
parents c1d94e5cd5ea
children c16947991354
files libinterp/corefcn/data.cc libinterp/corefcn/sub2ind.cc libinterp/corefcn/toplev.cc libinterp/corefcn/utils.cc libinterp/octave-value/ov-base-mat.cc libinterp/octave-value/ov-complex.cc libinterp/parse-tree/pt-assign.cc libinterp/parse-tree/pt-idx.cc liboctave/array/Array-util.cc liboctave/util/lo-array-gripes.cc liboctave/util/lo-array-gripes.h
diffstat 11 files changed, 84 insertions(+), 66 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/data.cc	Wed Oct 21 15:23:08 2015 -0400
+++ b/libinterp/corefcn/data.cc	Wed Oct 21 23:43:46 2015 -0400
@@ -82,6 +82,12 @@
 #define HAVE_HYPOTF 1
 #endif
 
+static void
+index_error (const char *fmt, const std::string& idx, const std::string& msg)
+{
+  error (fmt, idx.c_str (), msg.c_str ());
+}
+
 #define ANY_ALL(FCN) \
  \
   octave_value retval; \
@@ -7134,8 +7140,8 @@
         }
       catch (const index_exception& e)
         {
-          error ("nth_element: invalid N value %s. %s",
-                 e.idx (), e.explain ());
+          index_error ("nth_element: invalid N value %s. %s",
+                       e.idx (), e.details ());
         }
     }
   else
@@ -7216,8 +7222,8 @@
         }
       catch (const index_exception& e)
         {
-          error ("__accumarray_sum__: invalid IDX %s. %s",
-                 e.idx (), e.explain ());
+          index_error ("__accumarray_sum__: invalid IDX %s. %s",
+                       e.idx (), e.details ());
         }
     }
   else
@@ -7319,8 +7325,8 @@
         }
       catch (const index_exception& e)
         {
-          error ("do_accumarray_minmax_fun: invalid index %s. %s",
-                 e.idx (), e.explain ());
+          index_error ("do_accumarray_minmax_fun: invalid index %s. %s",
+                       e.idx (), e.details ());
         }
 
     }
@@ -7425,8 +7431,8 @@
         }
       catch (const index_exception& e)
         {
-          error ("__accumdim_sum__: invalid IDX %s. %s",
-                 e.idx (), e.explain ());
+          index_error ("__accumdim_sum__: invalid IDX %s. %s",
+                       e.idx (), e.details ());
         }
     }
   else
--- a/libinterp/corefcn/sub2ind.cc	Wed Oct 21 15:23:08 2015 -0400
+++ b/libinterp/corefcn/sub2ind.cc	Wed Oct 21 23:43:46 2015 -0400
@@ -108,7 +108,8 @@
                 {
                   e.set_pos_if_unset (nargin-1, j+1);
                   e.set_var ();
-                  error_with_id (e.id (), e.err ());
+                  std::string msg = e.message ();
+                  error_with_id (e.err_id (), msg.c_str ());
                 }
             }
           else
@@ -205,7 +206,9 @@
         }
       catch (const index_exception& e)
         {
-          error ("ind2sub: Invalid index %s. %s", e.idx (), e.explain ());
+          std::string idx = e.idx ();
+          std::string msg = e.details ();
+          error ("ind2sub: Invalid index %s. %s", idx.c_str (), msg.c_str ());
         }
     }
 
--- a/libinterp/corefcn/toplev.cc	Wed Oct 21 15:23:08 2015 -0400
+++ b/libinterp/corefcn/toplev.cc	Wed Oct 21 23:43:46 2015 -0400
@@ -662,7 +662,7 @@
         {
           recover_from_exception ();
           std::cerr << "error: unhandled index exception: "
-                    << e.err () << " -- trying to return to prompt"
+                    << e.message () << " -- trying to return to prompt"
                     << std::endl;
         }
       catch (const octave_execution_exception&)
--- a/libinterp/corefcn/utils.cc	Wed Oct 21 15:23:08 2015 -0400
+++ b/libinterp/corefcn/utils.cc	Wed Oct 21 23:43:46 2015 -0400
@@ -1255,11 +1255,11 @@
 }
 
 octave_idx_type
-dims_to_numel (const dim_vector& dims, const octave_value_list& idx)
+dims_to_numel (const dim_vector& dims, const octave_value_list& idx_arg)
 {
   octave_idx_type retval;
 
-  octave_idx_type len = idx.length ();
+  octave_idx_type len = idx_arg.length ();
 
   if (len == 0)
     retval = dims.numel ();
@@ -1269,7 +1269,7 @@
       retval = 1;
       for (octave_idx_type i = 0; i < len; i++)
         {
-          octave_value idxi = idx(i);
+          octave_value idxi = idx_arg(i);
           if (idxi.is_magic_colon ())
             retval *= dv(i);
           else if (idxi.is_numeric_type ())
@@ -1284,8 +1284,10 @@
                 }
               catch (const index_exception& e)
                 {
+                  std::string idx = e.idx ();
+                  std::string msg = e.details ();
                   error ("dims_to_numel: Invalid IDX %s. %s",
-                         e.idx (), e.explain ());
+                         idx.c_str (), msg.c_str ());
                 }
             }
         }
--- a/libinterp/octave-value/ov-base-mat.cc	Wed Oct 21 15:23:08 2015 -0400
+++ b/libinterp/octave-value/ov-base-mat.cc	Wed Oct 21 23:43:46 2015 -0400
@@ -370,7 +370,7 @@
   catch (const index_exception& e)
     {
       gripe_invalid_index (e.idx (), n_idx, k+1);
-     }
+    }
 
   // Clear cache.
   clear_cached_info ();
--- a/libinterp/octave-value/ov-complex.cc	Wed Oct 21 15:23:08 2015 -0400
+++ b/libinterp/octave-value/ov-complex.cc	Wed Oct 21 23:43:46 2015 -0400
@@ -486,17 +486,18 @@
 {
 public:
 
-  complex_index_exception (const char *value) : index_exception (value) { }
+  complex_index_exception (const std::string& value)
+    : index_exception (value) { }
 
   ~complex_index_exception (void) { }
 
-  const char* explain (void) const
+  std::string details (void) const
   {
     return "subscripts must be real (forgot to initialize i or j?)";
   }
 
   // ID of error to throw.
-  const char* id (void) const
+  const char *err_id (void) const
   {
     return error_id_invalid_index;
   }
--- a/libinterp/parse-tree/pt-assign.cc	Wed Oct 21 15:23:08 2015 -0400
+++ b/libinterp/parse-tree/pt-assign.cc	Wed Oct 21 23:43:46 2015 -0400
@@ -135,7 +135,8 @@
           catch (index_exception& e)
             {
               e.set_var (lhs->name ());
-              error_with_id (e.id (), e.err ());
+              std::string msg = e.message ();
+              error_with_id (e.err_id (), msg.c_str ());
             }
         }
     }
--- a/libinterp/parse-tree/pt-idx.cc	Wed Oct 21 15:23:08 2015 -0400
+++ b/libinterp/parse-tree/pt-idx.cc	Wed Oct 21 23:43:46 2015 -0400
@@ -280,7 +280,8 @@
       && dynamic_cast<const tree_identifier *> (expr)->is_variable ())
     e.set_var (expr->name ());
 
-  error_with_id (e.id (), e.err ());
+  std::string msg = e.message ();
+  error_with_id (e.err_id (), msg.c_str ());
 }
 
 octave_value_list
--- a/liboctave/array/Array-util.cc	Wed Oct 21 15:23:08 2015 -0400
+++ b/liboctave/array/Array-util.cc	Wed Oct 21 23:43:46 2015 -0400
@@ -563,8 +563,10 @@
           catch (index_exception& e)
             {
               e.set_pos_if_unset (len, i+1);
-              e.set_var ("");           // no particular variable
-              (*current_liboctave_error_with_id_handler) (e.id(), e.err());
+              e.set_var ();
+              std::string msg = e.message ();
+              (*current_liboctave_error_with_id_handler)
+                (e.err_id (), msg.c_str ());
             }
         }
       // idxa known to be valid. Shouldn't need to catch index_exception below here.
--- a/liboctave/util/lo-array-gripes.cc	Wed Oct 21 15:23:08 2015 -0400
+++ b/liboctave/util/lo-array-gripes.cc	Wed Oct 21 23:43:46 2015 -0400
@@ -25,7 +25,8 @@
 #include <config.h>
 #endif
 
-#include <string.h>
+#include <cstring>
+
 #include "lo-array-gripes.h"
 #include "lo-error.h"
 
@@ -107,19 +108,19 @@
 // object is indexed incorrectly, such as by an index that is out of
 // range, negative, fractional, complex, or of a non-numeric type.
 
-const char *
-index_exception::err (void) throw ()
+std::string
+index_exception::message (void) const
 {
-  msg = access () + "; " + explain ();
+  std::string msg = expression () + "; " + details ();
   return msg.c_str ();
 }
 
-// Show what was illegally accessed, e.g.,  "A(-1,_)", "A(0+1i)", "A(_,3)"
-// Show how many indices come before/after the offending one,
-// e.g., (<error>), (<error>,_), or (_,<error>,...[x5]...)
+// Show the expression that caused the error, e.g.,  "A(-1,_)",
+// "A(0+1i)", "A(_,3)".  Show how many indices come before/after the
+// offending one, e.g., (<error>), (<error>,_), or (_,<error>,...[x5]...)
 
 std::string
-index_exception:: access (void) const
+index_exception::expression (void) const
 {
   // FIXME: don't use a fixed size buffer!
   const int buf_len = 300;
@@ -182,7 +183,8 @@
   else
     v = var.c_str ();
 
-  snprintf (output, buf_len, "%s%s%s%s", v, pre, idx(), post);
+  std::string tmp_idx = idx ();
+  snprintf (output, buf_len, "%s%s%s%s", v, pre, tmp_idx.c_str (), post);
 
   return output;
 }
@@ -191,12 +193,12 @@
 {
 public:
 
-  invalid_index (const char *value, octave_idx_type ndim,
+  invalid_index (const std::string& value, octave_idx_type ndim,
                  octave_idx_type dimen)
     : index_exception (value, ndim, dimen)
   { }
 
-  const char* explain (void) const
+  std::string details (void) const
   {
 #ifdef USE_64_BIT_IDX_T
     return "subscripts must be either integers 1 to (2^63)-1 or logicals";
@@ -206,17 +208,17 @@
   }
 
   // ID of error to throw
-  const char* id (void) const
+  const char *err_id (void) const
   {
     return error_id_invalid_index;
   }
 };
 
-// Complain of an index that is: negative, fractional, or too big.
+// Complain if an index is negative, fractional, or too big.
 
 void
-gripe_invalid_index (const char *idx, octave_idx_type nd,
-                     octave_idx_type dim, const char * /* var */)
+gripe_invalid_index (const std::string& idx, octave_idx_type nd,
+                     octave_idx_type dim, const std::string&)
 {
     invalid_index e (idx, nd, dim);
 
@@ -225,9 +227,9 @@
 
 void
 gripe_invalid_index (octave_idx_type n, octave_idx_type nd,
-                     octave_idx_type dim, const char *var)
+                     octave_idx_type dim, const std::string& var)
 {
-  // Note: log10 (2^63) = 19 digits.  Use 64 for ease of memory alignment. 
+  // Note: log10 (2^63) = 19 digits.
   char buf[64];
 
   sprintf (buf, "%d", n+1);
@@ -237,7 +239,7 @@
 
 void
 gripe_invalid_index (double n, octave_idx_type nd, octave_idx_type dim,
-                     const char *var)
+                     const std::string& var)
 {
   char buf[64];
 
@@ -253,14 +255,14 @@
 {
 public:
 
-  out_of_range (const char *value, octave_idx_type nd_in,octave_idx_type dim_in)
-        : index_exception (value, nd_in, dim_in), extent(0)
-    { }
+  out_of_range (const std::string& value, octave_idx_type nd_in,
+                octave_idx_type dim_in)
+    : index_exception (value, nd_in, dim_in), extent (0)
+  { }
 
-  const char* explain (void) const
+  std::string details (void) const
   {
-    static std::string expl;    // should probably be member variable, but
-                                // then explain can't be const.
+    std::string expl;
 
     if (nd >= size.length ())   // if not an index slice
       {
@@ -278,13 +280,13 @@
         expl = "out of bound " + std::string (buf);
       }
 
-    return expl.c_str ();
+    return expl;
   }
 
   // ID of error to throw.
-  const char* id (void) const
+  const char *err_id (void) const
   {
-    return (error_id_index_out_of_bounds);
+    return error_id_index_out_of_bounds;
   }
 
   void set_size (const dim_vector& size_in) { size = size_in; }
--- a/liboctave/util/lo-array-gripes.h	Wed Oct 21 15:23:08 2015 -0400
+++ b/liboctave/util/lo-array-gripes.h	Wed Oct 21 23:43:46 2015 -0400
@@ -37,24 +37,24 @@
 {
 public:
 
-  index_exception (const char *index_in, octave_idx_type nd_in = 0,
+  index_exception (const std::string& index_in, octave_idx_type nd_in = 0,
                    octave_idx_type dim_in = 0, const char *var_in = "")
     : index (index_in), nd (nd_in), dim (dim_in), var (var_in)
   { }
 
-  ~index_exception (void) throw () { }
+  ~index_exception (void) { }
 
   // Erroneous index value.  Called in what, and by external code
   // (e.g., nth_element) to make a custom error message.
-  const char *idx (void) const { return index.c_str (); }
+  std::string idx (void) const { return index; }
 
   // details set by subclass.
-  virtual const char* explain (void) const = 0;
+  virtual std::string details (void) const = 0;
 
   // ID of error to throw.
-  virtual const char* id (void) const = 0;
+  virtual const char *err_id (void) const = 0;
 
-  virtual const char* err (void) throw ();
+  virtual std::string message (void) const;
 
   // Position of error: dimension in error, and number of dimensions.
   void set_pos (octave_idx_type nd_in, octave_idx_type dim_in)
@@ -80,13 +80,10 @@
   // Value of invalid index.
   std::string index;
 
-  // Formatted message returned by what(), (not on stack).
-  std::string msg;      
-
 protected:
 
   // Show what's wrong, e.g.,  A(-1,_), A(0+1i).
-  std::string access (void) const;
+  std::string expression (void) const;
 
   // Number of dimensions of indexed object.
   octave_idx_type nd;
@@ -143,16 +140,19 @@
                               octave_idx_type ext);
 
 extern void OCTAVE_API
-gripe_invalid_index (double, octave_idx_type nd=0,
-                     octave_idx_type dim=0, const char *var = NULL);
+gripe_invalid_index (double, octave_idx_type nd = 0,
+                     octave_idx_type dim = 0,
+                     const std::string& var = std::string ());
 
 extern void OCTAVE_API
-gripe_invalid_index (octave_idx_type n, octave_idx_type nd=0,
-                     octave_idx_type dim=0, const char *var = NULL);
+gripe_invalid_index (octave_idx_type n, octave_idx_type nd = 0,
+                     octave_idx_type dim = 0,
+                     const std::string& var = std::string ());
 
 extern void OCTAVE_API
-gripe_invalid_index (const char *idx, octave_idx_type nd=0,
-                     octave_idx_type dim=0, const char *var = NULL);
+gripe_invalid_index (const std::string& idx, octave_idx_type nd = 0,
+                     octave_idx_type dim = 0,
+                     const std::string& var = std::string ());
 
 extern void OCTAVE_API
 gripe_invalid_resize (void);