octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Octave warnings


From: John W. Eaton
Subject: Re: Octave warnings
Date: Wed, 19 Sep 2012 22:24:52 -0400

On 19-Sep-2012, Juan Pablo Carbajal wrote:

| On Wed, Sep 19, 2012 at 10:09 PM, Jordi GutiƩrrez Hermoso
| <address@hidden> wrote:
| > On 19 September 2012 15:34, Judd Storrs <address@hidden> wrote:
| >> Setting a COMPILE_OPT only affects the current scope--it doesn't affect the
| >> scope that called or any of the scopes that it calls, etc. The brilliance 
of
| >> this is that you can easily mix and mash code that conforms to old or new
| >> and not worry about new language features breaking legacy libraries.
| >
| > During OctConf 2012, there was talk about this:
| >
| >     http://wiki.octave.org/OctConf_TODO
| >
| > Consider this an invitation to revive this effort.
| >
| > - Jordi G. H.
| 
| There is more info on the topic
| 
| http://octave.1599824.n4.nabble.com/Warning-function-interface-td4631727.html
| 
| I do not know if anybody implemented anything.

My attempt is attached.  It seems to work but it needs some testing
and could probably be improved.

BTW, getting location info for warnings is already implemented.  You
just need to do

  warning on backtrace

Maybe we should make this the default?  I don't know.  It could result
in lots of info being dumped to the terminal.  The backtrace option
needs to be documented.  Anyone?

jwe


# HG changeset patch
# User John W. Eaton <address@hidden>
# Date 1348107660 14400
# Node ID 769d01898666b36be241cc2115813d3f6c3584f1
# Parent  53d073233fa44f81ab0c8224feda0376c6b39ea2
implement local option for warnings

* error.cc (Fwarning): Handle "local" option when setting warning
states.
* ov-usr-fcn.cc (octave_usr_function::restore_warning_states):
New private function.
(octave_usr_function::bind_automatic_vars): Create hidden auto
variable .saved_warning_states..
(octave_usr_function::do_multi_index_op): Insert pointer to
restore_warning_states function in unwind_protect frame.
* ov-usr-fcn.h (octave_usr_function::restore_warning_states):
Provide decl.
* NEWS: Note user visible change.

diff --git a/NEWS b/NEWS
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,17 @@
 
  ** A new GUI is now available with Octave.
 
+ ** Warning states may now be set temporarily, until the end of the
+    current function, using the syntax
+
+      warning STATE ID "local"
+
+    in which STATE may be "on", "off", or "error".  Changes to warning
+    states that are set locally affect the current function and all
+    functions called from the current scope.  The previous warning state
+    is restored on return from the current function.  The "local"
+    option is ignored if used in the top-level workspace.
+
 Summary of important user-visible changes for version 3.8:
 ---------------------------------------------------------
 
diff --git a/libinterp/interpfcn/error.cc b/libinterp/interpfcn/error.cc
--- a/libinterp/interpfcn/error.cc
+++ b/libinterp/interpfcn/error.cc
@@ -1149,6 +1149,7 @@
 @deftypefnx {Built-in Function} {} warning (\"off\", @var{id})\n\
 @deftypefnx {Built-in Function} {} warning (\"query\", @var{id})\n\
 @deftypefnx {Built-in Function} {} warning (\"error\", @var{id})\n\
address@hidden {Built-in Function} {} warning (@var{state}, @var{id}, 
\"local\")\n\
 Format the optional arguments under the control of the template string\n\
 @var{template} using the same rules as the @code{printf} family of\n\
 functions (@pxref{Formatted Output}) and print the resulting message\n\
@@ -1176,6 +1177,15 @@
 @end group\n\
 @end example\n\
 \n\
+If the state is @samp{\"on\"}, @samp{\"off\"}, or @samp{\"error\"}\n\
+and the third argument is @samp{\"local\"}, then the warning state\n\
+will be set temporarily, until the end of the current function.\n\
+Changes to warning states that are set locally affect the current\n\
+function and all functions called from the current scope.  The\n\
+previous warning state is restored on return from the current\n\
+function.  The \"local\" option is ignored if used in the top-level\n\
+workspace.\n\
+\n\
 Implementation Note: For compatibility with @sc{matlab}, escape\n\
 sequences (e.g., \"\\n\" => newline) are processed in @var{template}\n\
 regardless of whether @var{template} has been defined within single quotes\n\
@@ -1201,13 +1211,100 @@
           std::string arg1 = argv(1);
           std::string arg2 = "all";
 
-          if (argc == 3)
+          if (argc >= 3)
             arg2 = argv(2);
 
           if (arg1 == "on" || arg1 == "off" || arg1 == "error")
             {
               octave_map old_warning_options = warning_options;
 
+              if (argc == 4 && argv(3) == "local"
+                  && ! symbol_table::at_top_level ())
+                {
+                  symbol_table::scope_id scope
+                    = octave_call_stack::current_scope ();
+
+                  symbol_table::context_id context
+                    = octave_call_stack::current_context ();
+
+                  // FIXME -- it might be better to extract the bit of
+                  // code below that handles the query case and put it
+                  // in a separate function instead of calling Fwarning
+                  // recursively.
+
+                  octave_value_list tmp = Fwarning (ovl ("query", arg2), 1);
+
+                  octave_scalar_map val = tmp(0).scalar_map_value ();
+
+                  octave_value curr_state = val.contents ("state");
+
+                  // FIXME -- this might be better with a dictionary
+                  // object.
+
+                  octave_value curr_warning_states
+                    = symbol_table::varval (".saved_warning_states.",
+                                            scope, context);
+
+                  octave_map m;
+
+                  if (curr_warning_states.is_defined ())
+                    m = curr_warning_states.map_value ();
+                  else
+                    {
+                      string_vector fields (2);
+
+                      fields(0) = "identifier";
+                      fields(1) = "state";
+
+                      m = octave_map (dim_vector (0, 1), fields);
+                    }
+
+                  if (error_state)
+                    panic_impossible ();
+
+                  Cell ids = m.contents ("identifier");
+                  Cell states = m.contents ("state");
+
+                  octave_idx_type nel = states.numel ();
+                  bool found = false;
+                  octave_idx_type i;
+                  for (i = 0; i < nel; i++)
+                    {
+                      std::string id = ids(i).string_value ();
+
+                      if (error_state)
+                        panic_impossible ();
+
+                      if (id == arg2)
+                        {
+                          states(i) = curr_state;
+                          found = true;
+                          break;
+                        }
+                    }
+
+                  if (! found)
+                    {
+                      m.resize (dim_vector (nel+1, 1));
+
+                      ids.resize (nel+1);
+                      states.resize (nel+1);
+
+                      ids(nel) = arg2;
+                      states(nel) = curr_state;
+                    }
+
+                  m.contents ("identifier") = ids;
+                  m.contents ("state") = states;
+
+                  symbol_table::varref
+                    (".saved_warning_states.", scope, context) = m;
+
+                  // Now ignore the "local" argument and continue to
+                  // handle the current setting.
+                  argc--;
+                }
+                  
               if (arg2 == "all")
                 {
                   octave_map tmp;
diff --git a/libinterp/octave-value/ov-usr-fcn.cc 
b/libinterp/octave-value/ov-usr-fcn.cc
--- a/libinterp/octave-value/ov-usr-fcn.cc
+++ b/libinterp/octave-value/ov-usr-fcn.cc
@@ -30,6 +30,7 @@
 
 #include <defaults.h>
 #include "Cell.h"
+#include "builtins.h"
 #include "defun.h"
 #include "error.h"
 #include "gripes.h"
@@ -455,6 +456,8 @@
   bind_automatic_vars (arg_names, nargin, nargout, all_va_args (args),
                        lvalue_list);
 
+  frame.add_method (this, &octave_user_function::restore_warning_states);
+
   bool echo_commands = (Vecho_executing_commands & ECHO_FUNCTIONS);
 
   if (echo_commands)
@@ -614,6 +617,11 @@
   symbol_table::mark_automatic (".nargin.");
   symbol_table::mark_automatic (".nargout.");
 
+  symbol_table::varref (".saved_warning_states.") = octave_value ();
+
+  symbol_table::mark_automatic (".saved_warning_states.");
+  symbol_table::mark_automatic (".saved_warning_states.");
+
   if (takes_varargs ())
     symbol_table::varref ("varargin") = va_args.cell_value ();
 
@@ -648,6 +656,26 @@
   symbol_table::mark_automatic (".ignored.");
 }
 
+void
+octave_user_function::restore_warning_states (void)
+{
+  octave_value val = symbol_table::varval (".saved_warning_states.");
+
+  if (val.is_defined ())
+    {
+      octave_map m = val.map_value ();
+
+      if (error_state)
+        panic_impossible ();
+
+      Cell ids = m.contents ("identifier");
+      Cell states = m.contents ("state");
+
+      for (octave_idx_type i = 0; i < m.numel (); i++)
+        Fwarning (ovl (states(i), ids(i)));
+    }
+}
+
 DEFUN (nargin, args, ,
   "-*- texinfo -*-\n\
 @deftypefn  {Built-in Function} {} nargin ()\n\
diff --git a/libinterp/octave-value/ov-usr-fcn.h 
b/libinterp/octave-value/ov-usr-fcn.h
--- a/libinterp/octave-value/ov-usr-fcn.h
+++ b/libinterp/octave-value/ov-usr-fcn.h
@@ -476,6 +476,8 @@
                             int nargout, const octave_value_list& va_args,
                             const std::list<octave_lvalue> *lvalue_list);
 
+  void restore_warning_states (void);
+
   // No copying!
 
   octave_user_function (const octave_user_function& fn);

reply via email to

[Prev in Thread] Current Thread [Next in Thread]