changeset 10605:1834132fb50b

allow non-integer ranges as indices conditionally
author John W. Eaton <jwe@octave.org>
date Wed, 05 May 2010 14:20:58 -0400
parents d7ff75c977e2
children ec34c7acd057
files src/ChangeLog src/error.cc src/error.h src/octave.cc src/ov-range.cc src/ov-range.h src/utils.cc
diffstat 7 files changed, 86 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Tue May 04 12:56:27 2010 -0400
+++ b/src/ChangeLog	Wed May 05 14:20:58 2010 -0400
@@ -1,3 +1,17 @@
+2010-05-05  John W. Eaton  <jwe@octave.org>
+
+	* utils.cc (reset_warning_state): New function.
+	(Fisindex): Temporarily set warning state for
+	Octave:allow-noninteger-ranges-as-indices to "error".
+	* error.cc (set_warning_state): New function.
+	(initialize_default_warning_state): Set default warning state
+	for Octave:allow-noninteger-ranges-as-indices to "error".
+	* error.h (set_warning_state): Provide decl.
+	* octave.cc (maximum_braindamage): Set warning state for
+	Octave:allow-noninteger-ranges-as-indices to "on".
+	* ov-range.cc (octave_range::index_vector): Warn if range
+	contains non-integer values.
+
 2010-05-03  Jaroslav Hajek  <highegg@gmail.com>
 
 	* DLD-FUNCTIONS/svd.cc (driver): New static var.
--- a/src/error.cc	Tue May 04 12:56:27 2010 -0400
+++ b/src/error.cc	Wed May 05 14:20:58 2010 -0400
@@ -1440,15 +1440,27 @@
   return retval;
 }
 
-void
-disable_warning (const std::string& id)
+octave_value_list
+set_warning_state (const std::string& id, const std::string& state)
 {
   octave_value_list args;
 
   args(1) = id;
-  args(0) = "off";
+  args(0) = state;
+
+  return Fwarning (args, 1);
+}
 
-  Fwarning (args, 0);
+octave_value_list
+set_warning_state (const octave_value_list& args)
+{
+  return Fwarning (args, 1);
+}
+
+void
+disable_warning (const std::string& id)
+{
+  set_warning_state (id, "off");
 }
 
 void
@@ -1473,6 +1485,10 @@
   disable_warning ("Octave:string-concat");
   disable_warning ("Octave:variable-switch-label");
   disable_warning ("Octave:complex-cmp-ops");
+
+  // This should be an error unless we are in maximum braindamage mode.
+
+  set_warning_state ("Octave:allow-noninteger-ranges-as-indices", "error");
 }
 
 DEFUN (lasterror, args, ,
--- a/src/error.h	Tue May 04 12:56:27 2010 -0400
+++ b/src/error.h	Wed May 05 14:20:58 2010 -0400
@@ -27,6 +27,8 @@
 #include <cstdarg>
 #include <string>
 
+class octave_value_list;
+
 #define panic_impossible() \
   panic ("impossible state reached in file `%s' at line %d", \
          __FILE__, __LINE__)
@@ -94,6 +96,12 @@
 // Helper function for print_usage defined in defun.cc.
 extern OCTINTERP_API void defun_usage_message (const std::string& msg);
 
+extern OCTINTERP_API octave_value_list
+set_warning_state (const std::string& id, const std::string& state);
+
+extern OCTINTERP_API octave_value_list
+set_warning_state (const octave_value_list& args);
+
 extern OCTINTERP_API void disable_warning (const std::string& id);
 extern OCTINTERP_API void initialize_default_warning_state (void);
 
--- a/src/octave.cc	Tue May 04 12:56:27 2010 -0400
+++ b/src/octave.cc	Wed May 05 14:20:58 2010 -0400
@@ -573,6 +573,8 @@
   bind_internal_variable ("page_screen_output", false);
   bind_internal_variable ("print_empty_dimensions", false);
 
+  set_warning_state ("Octave:allow-noninteger-ranges-as-indices", "on");
+
   disable_warning ("Octave:abbreviated-property-match");
   disable_warning ("Octave:fopen-file-in-path");
   disable_warning ("Octave:function-name-clash");
--- a/src/ov-range.cc	Tue May 04 12:56:27 2010 -0400
+++ b/src/ov-range.cc	Wed May 05 14:20:58 2010 -0400
@@ -30,6 +30,8 @@
 #include "lo-ieee.h"
 #include "lo-utils.h"
 
+#include "defun.h"
+#include "variables.h"
 #include "gripes.h"
 #include "ops.h"
 #include "oct-obj.h"
@@ -142,6 +144,25 @@
     }
 }
 
+idx_vector
+octave_range::index_vector (void) const
+{
+  if (idx_cache)
+    return *idx_cache;
+  else
+    {
+      if (range.all_elements_are_ints ())
+        return set_idx_cache (idx_vector (range));
+      else
+        {
+          warning_with_id ("Octave:allow-noninteger-ranges-as-indices",
+                           "rounding non-integer range used as index to nearest integer");
+
+          return octave_value (matrix_value ()).round ().index_vector ();
+        }
+    }
+}
+
 double
 octave_range::double_value (bool) const
 {
--- a/src/ov-range.h	Tue May 04 12:56:27 2010 -0400
+++ b/src/ov-range.h	Wed May 05 14:20:58 2010 -0400
@@ -106,8 +106,7 @@
   octave_value do_index_op (const octave_value_list& idx,
                             bool resize_ok = false);
 
-  idx_vector index_vector (void) const 
-    { return idx_cache ? *idx_cache : set_idx_cache (idx_vector (range)); }
+  idx_vector index_vector (void) const;
 
   dim_vector dims (void) const
     { 
--- a/src/utils.cc	Tue May 04 12:56:27 2010 -0400
+++ b/src/utils.cc	Wed May 05 14:20:58 2010 -0400
@@ -60,6 +60,7 @@
 #include "oct-hist.h"
 #include "oct-obj.h"
 #include "pager.h"
+#include "parse.h"
 #include "sysdep.h"
 #include "toplev.h"
 #include "unwind-prot.h"
@@ -1303,6 +1304,17 @@
     }
 }
 
+// FIXME -- is there some way to fix the declarations in unwind-prot.h
+// so that this function's argument can be declared as
+// "const octave_value_list&"?
+
+static void
+reset_warning_state (octave_value_list args)
+{
+  if (! args.empty ())
+    set_warning_state (args);
+}
+
 DEFUN (isindex, args, ,
   "-*- texinfo -*-\n\
 @deftypefn {Built-in Function} {} isindex (@var{ind}, @var{n})\n\
@@ -1325,7 +1337,15 @@
   if (! error_state)
     {
       unwind_protect frame;
+
+      octave_value_list current_warning_state
+        = set_warning_state ("Octave:allow-noninteger-ranges-as-indices",
+                             "error");
+
+      frame.add_fcn (reset_warning_state, current_warning_state);
+
       frame.protect_var (error_state);
+
       frame.protect_var (discard_error_messages);
       discard_error_messages = true;