# HG changeset patch # User Rik # Date 1454390179 28800 # Node ID 1edaf1ee65eb0d65fd0102cf39ef9f35df2d9630 # Parent 40051830f89b57806e20d164e8022661f4b9a1f4 maint: Replace some uses of goto in libinterp. * ls-mat4.cc (read_mat_file_header): Replace "goto data_read_error" with "return -1". * ls-mat4.cc (read_mat_binary_data): Replace "goto data_read_error" with direct call to error. Move variable declarations to point of usage. * ls-mat5.cc (read_mat5_tag, write_mat5_tag): Replace "goto data_read_error" with "return 1". * ls-mat5.cc (save_mat5_binary_element): Replace "goto skip_to_next" with "return true". * ls-oct-binary.cc (read_binary_data): Replace "goto data_read_error" with direct call to error. * mex.cc (valid_key): Replace "goto done" with "return retval". * rand.cc (do_rand): Clarify comment. * pt-binop.cc (tree_binary_expression::rvalue1): Replace "goto done" with "return octave_value (xxx)" * pt-binop.cc (tree_boolean_expression::rvalue1): Replace "goto done" with "return octave_value (xxx)" diff -r 40051830f89b -r 1edaf1ee65eb libinterp/corefcn/ls-mat4.cc --- a/libinterp/corefcn/ls-mat4.cc Mon Feb 01 13:33:13 2016 -0800 +++ b/libinterp/corefcn/ls-mat4.cc Mon Feb 01 21:16:19 2016 -0800 @@ -125,16 +125,16 @@ return 1; if (! is.read (reinterpret_cast (&nr), 4)) - goto data_read_error; + return -1; if (! is.read (reinterpret_cast (&nc), 4)) - goto data_read_error; + return -1; if (! is.read (reinterpret_cast (&imag), 4)) - goto data_read_error; + return -1; if (! is.read (reinterpret_cast (&len), 4)) - goto data_read_error; + return -1; // If mopt is nonzero and the byte order is swapped, mopt will be // bigger than we expect, so we swap bytes. @@ -171,9 +171,6 @@ } return 0; - -data_read_error: - return -1; } // We don't just use a cast here, because we need to be able to detect @@ -243,30 +240,23 @@ { std::string retval; - // These are initialized here instead of closer to where they are - // first used to avoid errors from gcc about goto crossing - // initialization of variable. - - Matrix re; - oct_mach_info::float_format flt_fmt = oct_mach_info::flt_fmt_unknown; bool swap = false; - int type = 0; - int prec = 0; - int order = 0; - int mach = 0; - int dlen = 0; - int32_t mopt, nr, nc, imag, len; int err = read_mat_file_header (is, swap, mopt, nr, nc, imag, len); if (err) { if (err < 0) - goto data_read_error; - else - return retval; + error ("load: trouble reading binary file '%s'", filename.c_str ()); + + return retval; } + int type = 0; + int prec = 0; + int order = 0; + int mach = 0; + type = mopt % 10; // Full, sparse, etc. mopt /= 10; // Eliminate first digit. prec = mopt % 10; // double, float, int, etc. @@ -275,6 +265,7 @@ mopt /= 10; // Eliminate third digit. mach = mopt % 10; // IEEE, VAX, etc. + oct_mach_info::float_format flt_fmt; flt_fmt = mopt_digit_to_float_format (mach); if (flt_fmt == oct_mach_info::flt_fmt_unknown) @@ -283,6 +274,8 @@ if (imag && type == 1) error ("load: encountered complex matrix with string flag set!"); + int dlen = 0; + // LEN includes the terminating character, and the file is also // supposed to include it, but apparently not all files do. Either // way, I think this should work. @@ -291,12 +284,12 @@ OCTAVE_LOCAL_BUFFER (char, name, len+1); name[len] = '\0'; if (! is.read (name, len)) - goto data_read_error; + error ("load: trouble reading binary file '%s'", filename.c_str ()); retval = name; dlen = nr * nc; if (dlen < 0) - goto data_read_error; + error ("load: trouble reading binary file '%s'", filename.c_str ()); if (order) { @@ -364,7 +357,7 @@ } else { - re.resize (nr, nc); + Matrix re (nr, nc); read_mat_binary_data (is, re.fortran_vec (), prec, dlen, swap, flt_fmt); @@ -385,7 +378,7 @@ for (octave_idx_type j = 0; j < nc; j++) for (octave_idx_type i = 0; i < nr; i++) - ctmp (i, j) = Complex (re (i, j), im (i, j)); + ctmp (i,j) = Complex (re(i,j), im(i,j)); tc = order ? ctmp.transpose () : ctmp; } @@ -398,11 +391,6 @@ return retval; } - -// FIXME: With short-circuiting error(), no need for goto in code -data_read_error: - error ("load: trouble reading binary file '%s'", filename.c_str ()); - } // Save the data from TC along with the corresponding NAME on stream OS diff -r 40051830f89b -r 1edaf1ee65eb libinterp/corefcn/ls-mat5.cc --- a/libinterp/corefcn/ls-mat5.cc Mon Feb 01 13:33:13 2016 -0800 +++ b/libinterp/corefcn/ls-mat5.cc Mon Feb 01 21:16:19 2016 -0800 @@ -429,7 +429,7 @@ int32_t temp; if (! is.read (reinterpret_cast (&temp), 4)) - goto data_read_error; + return 1; if (swap) swap_bytes<4> (&temp); @@ -446,7 +446,7 @@ else { if (! is.read (reinterpret_cast (&temp), 4)) - goto data_read_error; + return 1; if (swap) swap_bytes<4> (&temp); bytes = temp; @@ -454,9 +454,6 @@ } return 0; - -data_read_error: - return 1; } static void @@ -1561,17 +1558,14 @@ { temp = type; if (! is.write (reinterpret_cast (&temp), 4)) - goto data_write_error; + return 1; temp = bytes; } if (! is.write (reinterpret_cast (&temp), 4)) - goto data_write_error; + return 1; return 0; - -data_write_error: - return 1; } // Have to use copy here to avoid writing over data accessed via @@ -2255,7 +2249,7 @@ if (dv(i) > max_dim_val) { warn_dim_too_large (name); - goto skip_to_next; + return true; // skip to next } } @@ -2280,7 +2274,7 @@ if (nnz > max_dim_val || nc + 1 > max_dim_val) { warn_dim_too_large (name); - goto skip_to_next; + return true; // skip to next } nnz_32 = nnz; @@ -2288,7 +2282,7 @@ else if (dv.numel () > max_dim_val) { warn_dim_too_large (name); - goto skip_to_next; + return true; // skip to next } #ifdef HAVE_ZLIB @@ -2381,7 +2375,7 @@ { // FIXME: Should this just error out rather than warn? warn_wrong_type_arg ("save", tc); - goto error_cleanup; + error ("save: error while writing '%s' to MAT file", name.c_str ()); } os.write (reinterpret_cast (&flags), 4); @@ -2561,7 +2555,7 @@ Cell cell = tc.cell_value (); if (! write_mat5_cell_array (os, cell, mark_as_global, save_as_floats)) - goto error_cleanup; + error ("save: error while writing '%s' to MAT file", name.c_str ()); } else if (tc.is_complex_scalar () || tc.is_complex_matrix ()) { @@ -2613,7 +2607,8 @@ } catch (const octave_execution_exception&) { - goto error_cleanup; + error ("save: error while writing '%s' to MAT file", + name.c_str ()); } } else @@ -2667,7 +2662,8 @@ false, save_as_floats); if (! retval2) - goto error_cleanup; + error ("save: error while writing '%s' to MAT file", + name.c_str ()); } } } @@ -2676,11 +2672,5 @@ // FIXME: Should this just error out rather than warn? warn_wrong_type_arg ("save", tc); -skip_to_next: return true; - -// FIXME: With short-circuiting error(), no need for goto in code -error_cleanup: - error ("save: error while writing '%s' to MAT file", name.c_str ()); - } diff -r 40051830f89b -r 1edaf1ee65eb libinterp/corefcn/ls-oct-binary.cc --- a/libinterp/corefcn/ls-oct-binary.cc Mon Feb 01 13:33:13 2016 -0800 +++ b/libinterp/corefcn/ls-oct-binary.cc Mon Feb 01 21:16:19 2016 -0800 @@ -158,13 +158,13 @@ OCTAVE_LOCAL_BUFFER (char, name, name_len+1); name[name_len] = '\0'; if (! is.read (reinterpret_cast (name), name_len)) - goto data_read_error; + error ("load: trouble reading binary file '%s'", filename.c_str ()); retval = name; } is.read (reinterpret_cast (&doc_len), 4); if (! is) - goto data_read_error; + error ("load: trouble reading binary file '%s'", filename.c_str ()); if (swap) swap_bytes<4> (&doc_len); @@ -172,17 +172,17 @@ OCTAVE_LOCAL_BUFFER (char, tdoc, doc_len+1); tdoc[doc_len] = '\0'; if (! is.read (reinterpret_cast (tdoc), doc_len)) - goto data_read_error; + error ("load: trouble reading binary file '%s'", filename.c_str ()); doc = tdoc; } if (! is.read (reinterpret_cast (&tmp), 1)) - goto data_read_error; + error ("load: trouble reading binary file '%s'", filename.c_str ()); global = tmp ? 1 : 0; tmp = 0; if (! is.read (reinterpret_cast (&tmp), 1)) - goto data_read_error; + error ("load: trouble reading binary file '%s'", filename.c_str ()); // All cases except 255 kept for backwards compatibility switch (tmp) @@ -210,12 +210,12 @@ // this is taking backward compatibility too far!! int32_t len; if (! is.read (reinterpret_cast (&len), 4)) - goto data_read_error; + error ("load: trouble reading binary file '%s'", filename.c_str ()); if (swap) swap_bytes<4> (&len); OCTAVE_LOCAL_BUFFER (char, s, len+1); if (! is.read (reinterpret_cast (s), len)) - goto data_read_error; + error ("load: trouble reading binary file '%s'", filename.c_str ()); s[len] = '\0'; tc = s; @@ -237,27 +237,24 @@ // Read the saved variable type int32_t len; if (! is.read (reinterpret_cast (&len), 4)) - goto data_read_error; + error ("load: trouble reading binary file '%s'", filename.c_str ()); if (swap) swap_bytes<4> (&len); OCTAVE_LOCAL_BUFFER (char, s, len+1); if (! is.read (s, len)) - goto data_read_error; + error ("load: trouble reading binary file '%s'", filename.c_str ()); s[len] = '\0'; std::string typ = s; tc = octave_value_typeinfo::lookup_type (typ); } break; default: - goto data_read_error; + error ("load: trouble reading binary file '%s'", filename.c_str ()); break; } if (! tc.load_binary (is, swap, fmt)) - { - data_read_error: - error ("load: trouble reading binary file '%s'", filename.c_str ()); - } + error ("load: trouble reading binary file '%s'", filename.c_str ()); return retval; } diff -r 40051830f89b -r 1edaf1ee65eb libinterp/corefcn/mex.cc --- a/libinterp/corefcn/mex.cc Mon Feb 01 13:33:13 2016 -0800 +++ b/libinterp/corefcn/mex.cc Mon Feb 01 21:16:19 2016 -0800 @@ -91,15 +91,13 @@ for (int i = 1; i < nel; i++) { if (! (isalnum (key[i]) || key[i] == '_')) - goto done; + return retval; } retval = 1; } } -done: - return retval; } diff -r 40051830f89b -r 1edaf1ee65eb libinterp/corefcn/rand.cc --- a/libinterp/corefcn/rand.cc Mon Feb 01 13:33:13 2016 -0800 +++ b/libinterp/corefcn/rand.cc Mon Feb 01 21:16:19 2016 -0800 @@ -271,7 +271,7 @@ break; } - // No "goto gen_matrix" in code path. Must be done. + // No "goto gen_matrix" in code path. Must be done processing. return retval; gen_matrix: diff -r 40051830f89b -r 1edaf1ee65eb libinterp/parse-tree/pt-binop.cc --- a/libinterp/parse-tree/pt-binop.cc Mon Feb 01 13:33:13 2016 -0800 +++ b/libinterp/parse-tree/pt-binop.cc Mon Feb 01 21:16:19 2016 -0800 @@ -87,8 +87,7 @@ if (etype == octave_value::op_el_or) { matlab_style_short_circuit_warning ("|"); - result = true; - goto done; + return octave_value (true); } } else @@ -96,7 +95,7 @@ if (etype == octave_value::op_el_and) { matlab_style_short_circuit_warning ("&"); - goto done; + return octave_value (false); } } @@ -107,8 +106,6 @@ result = b.is_true (); } - done: - return octave_value (result); } } @@ -205,15 +202,12 @@ if (a_true) { if (etype == bool_or) - { - result = true; - goto done; - } + return octave_value (true); } else { if (etype == bool_and) - goto done; + return octave_value (false); } if (op_rhs) @@ -223,8 +217,6 @@ result = b.is_true (); } - done: - retval = octave_value (result); }