view libinterp/corefcn/error.h @ 20574:dd6345fd8a97

use exceptions for better invalid index error reporting (bug #45957) * lo-array-gripes.h, lo-array-gripes.cc (index_exception): New base class for indexing errors. (invalid_index, out_of_range): New classes. (gripe_index_out_of_range): New overloaded function. (gripe_invalid_index): New overloaded functions. Delete version with no arguments. (gripe_invalid_assignment_size, gripe_assignment_dimension_mismatch): Delete. Change uses of gripe functions as needed. * Cell.cc (Cell::index, Cell::assign, Cell::delete_elements): Use exceptions to collect error info about and handle indexing errors. * data.cc (Fnth_element, do_accumarray_sum, F__accumarray_sum__, do_accumarray_minmax, do_accumarray_minmax_fun, F__accumdim_sum__): Likewise. * oct-map.cc (octave_map::index, octave_map::assign, octave_map::delete_elements): Likewise. * sparse.cc (Fsparse): Likewise. * sub2ind.cc (Fsub2ind, Find2sub): Likewise. New tests. * utils.cc (dims_to_numel): Likewise. * ov-base-diag.cc (octave_base_diag<DMT, MT>::do_index_op, octave_base_diag<DMT, MT>::subsasgn): Likewise. * ov-base-mat.cc (octave_base_matrix<MT>::subsref, octave_base_matrix<MT>::assign): Likewise. * ov-base-sparse.cc (octave_base_sparse<T>::do_index_op, octave_base_sparse<T>::assign, octave_base_sparse<MT>::delete_elements): Likewise. * ov-classdef.cc (cdef_object_array::subsref, cdef_object_array::subsasgn): Likewise. * ov-java.cc (make_java_index): Likewise. * ov-perm.cc (octave_perm_matrix::do_index_op): Likewise. * ov-range.cc (octave_range::do_index_op): Likewise. * ov-re-diag.cc (octave_diag_matrix::do_index_op): Likewise. * ov-str-mat.cc (octave_char_matrix_str::do_index_op_internal): Likewise. * pt-assign.cc (tree_simple_assignment::rvalue1): Likewise. * pt-idx.cc (tree_index_expression::rvalue, tree_index_expression::lvalue): Likewise. * Array-util.cc (sub2ind): Likewise. * toplev.cc (main_loop): Also catch unhandled index_exception exceptions. * ov-base.cc (octave_base_value::index_vector): Improve error message. * ov-re-sparse.cc (octave_sparse_matrix::index_vector): Likewise. * ov-complex.cc (complex_index): New class. (gripe_complex_index): New function. (octave_complex::index_vector): Use it. * pt-id.h, pt-id.cc (tree_identifier::is_variable, tree_black_hole::is_variable): Now const. * pt-idx.cc (final_index_error): New static function. (tree_index_expression::rvalue, tree_index_expression::lvalue): Use it. * index.tst: New tests.
author Lachlan Andrew <lachlanbis@gmail.com>
date Fri, 02 Oct 2015 15:07:37 -0400
parents 19755f4fc851
children
line wrap: on
line source

/*

Copyright (C) 1993-2015 John W. Eaton

This file is part of Octave.

Octave is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version.

Octave is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with Octave; see the file COPYING.  If not, see
<http://www.gnu.org/licenses/>.

*/

#if !defined (octave_error_h)
#define octave_error_h 1

#include <cstdarg>
#include <string>

class octave_map;
class octave_value_list;
class unwind_protect;

#define panic_impossible() \
  panic ("impossible state reached in file '%s' at line %d", __FILE__, __LINE__)

extern OCTINTERP_API void reset_error_handler (void);

extern OCTINTERP_API int warning_enabled (const std::string& id);

extern OCTINTERP_API void
vmessage (const char *name, const char *fmt, va_list args);

extern OCTINTERP_API void message (const char *name, const char *fmt, ...);

extern OCTINTERP_API void vusage (const char *fmt, va_list args);
extern OCTINTERP_API void usage (const char *fmt, ...);

extern OCTINTERP_API void vwarning (const char *fmt, va_list args);
extern OCTINTERP_API void warning (const char *fmt, ...);

extern OCTINTERP_API void verror (const char *fmt, va_list args);
extern OCTINTERP_API void error (const char *fmt, ...);

extern OCTINTERP_API void verror_with_cfn (const char *fmt, va_list args);
extern OCTINTERP_API void error_with_cfn (const char *fmt, ...);

extern OCTINTERP_API void vparse_error (const char *fmt, va_list args);
extern OCTINTERP_API void parse_error (const char *fmt, ...);

extern OCTINTERP_API void
vmessage_with_id (const char *id, const char *name,
                  const char *fmt, va_list args);

extern OCTINTERP_API void
message_with_id (const char *id, const char *name, const char *fmt, ...);

extern OCTINTERP_API void
vusage_with_id (const char *id, const char *fmt, va_list args);

extern OCTINTERP_API void
usage_with_id (const char *id, const char *fmt, ...);

extern OCTINTERP_API void
vwarning_with_id (const char *id, const char *fmt, va_list args);

extern OCTINTERP_API void
warning_with_id (const char *id, const char *fmt, ...);

extern OCTINTERP_API void
verror_with_id (const char *id, const char *fmt, va_list args);

extern OCTINTERP_API void
error_with_id (const char *id, const char *fmt, ...);

extern OCTINTERP_API void
verror_with_id_cfn (const char *id, const char *fmt, va_list args);

extern OCTINTERP_API void
error_with_id_cfn (const char *id, const char *fmt, ...);

extern OCTINTERP_API void
vparse_error_with_id (const char *id, const char *fmt, va_list args);

extern OCTINTERP_API void
parse_error_with_id (const char *id, const char *fmt, ...);

extern OCTINTERP_API void panic (const char *fmt, ...) GCC_ATTR_NORETURN;

// 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);

// TRUE means that Octave will try to enter the debugger when an error
// is encountered.  This will also inhibit printing of the normal
// traceback message (you will only see the top-level error message).
extern OCTINTERP_API bool Vdebug_on_error;

// TRUE means that Octave will try to enter the debugger when a warning
// is encountered.
extern OCTINTERP_API bool Vdebug_on_warning;

// Current error state.
extern OCTINTERP_API int error_state;

// Current warning state.
extern OCTINTERP_API int warning_state;

// Tell the error handler whether to print messages, or just store
// them for later.  Used for handling errors in eval() and
// the 'unwind_protect' statement.
extern OCTINTERP_API int buffer_error_messages;

// TRUE means error messages are turned off.
extern OCTINTERP_API bool discard_error_messages;

// TRUE means warning messages are turned off.
extern OCTINTERP_API bool discard_warning_messages;

// Helper functions to pass last error and warning messages and ids
extern OCTINTERP_API std::string last_error_message (void);
extern OCTINTERP_API std::string last_error_id (void);
extern OCTINTERP_API octave_map last_error_stack (void);
extern OCTINTERP_API std::string last_warning_message (void);
extern OCTINTERP_API std::string last_warning_id (void);

extern OCTINTERP_API void interpreter_try (unwind_protect&);

#endif