# HG changeset patch # User John W. Eaton # Date 1712428697 14400 # Node ID 3606817b9994955f0b102b16e177837f863e04ae # Parent eb8a24370c2b86179fce4e15288bfd53a3fa8474 take adavantage of compiler warnings for unhandled enum switch cases Don't use default cases in switch statements that use enum values so that compilers may warn about unhandled enum values. Use error to report enum values that are not accepted by the switch, use error with a meaningful message instead of panic_impossible. diff -r eb8a24370c2b -r 3606817b9994 libinterp/corefcn/data.cc --- a/libinterp/corefcn/data.cc Sat Apr 06 18:44:45 2024 +0200 +++ b/libinterp/corefcn/data.cc Sat Apr 06 14:38:17 2024 -0400 @@ -656,8 +656,23 @@ #undef MAKE_INT_BRANCH - default: - panic_impossible (); + case btyp_double: + case btyp_float: + case btyp_complex: + case btyp_float_complex: + case btyp_bool: + case btyp_char: + case btyp_struct: + case btyp_cell: + case btyp_func_handle: + case btyp_unknown: + error ("rem: unexpected: found %s instead of integer - please report this bug", btyp_class_name[btyp0].c_str ()); + break; + + // We should have handled all possible enum values above. + // Rely on compiler diagnostics to warn if we haven't. For + // example, GCC's -Wswitch option, enabled by -Wall, will + // provide a warning. } } else if (args(0).is_single_type () || args(1).is_single_type ()) @@ -840,8 +855,23 @@ #undef MAKE_INT_BRANCH - default: - panic_impossible (); + case btyp_double: + case btyp_float: + case btyp_complex: + case btyp_float_complex: + case btyp_bool: + case btyp_char: + case btyp_struct: + case btyp_cell: + case btyp_func_handle: + case btyp_unknown: + error ("mod: unexpected: found %s instead of integer - please report this bug", btyp_class_name[btyp0].c_str ()); + break; + + // We should have handled all possible enum values above. + // Rely on compiler diagnostics to warn if we haven't. For + // example, GCC's -Wswitch option, enabled by -Wall, will + // provide a warning. } } else if (args(0).is_single_type () || args(1).is_single_type ()) diff -r eb8a24370c2b -r 3606817b9994 libinterp/corefcn/mex.cc --- a/libinterp/corefcn/mex.cc Sat Apr 06 18:44:45 2024 +0200 +++ b/libinterp/corefcn/mex.cc Sat Apr 06 14:38:17 2024 -0400 @@ -1129,9 +1129,9 @@ mxClassID get_class_id () const { return m_id; } - const char * get_class_name () const + static std::string get_class_name (mxClassID id) { - switch (m_id) + switch (id) { case mxDOUBLE_CLASS: return "double"; case mxSINGLE_CLASS: return "single"; @@ -1154,6 +1154,11 @@ } } + const char * get_class_name () const + { + return m_class_name; + } + void set_class_name (const char *name) { mxFree (m_class_name); @@ -1621,7 +1626,9 @@ double retval = 0; - switch (get_class_id ()) + mxClassID id = get_class_id (); + + switch (id) { case mxDOUBLE_CLASS: retval = *(static_cast (m_pr)); @@ -1671,8 +1678,21 @@ retval = *(static_cast (m_pr)); break; - default: - panic_impossible (); + case mxCELL_CLASS: + case mxFUNCTION_CLASS: + case mxSTRUCT_CLASS: + case mxUNKNOWN_CLASS: + case mxVOID_CLASS: + { + std::string dest_cname = get_class_name (id); + error ("invalid conversion from %s mxArray to %s scalar value", get_class_name (), dest_cname.c_str ()); + } + break; + + // We should have handled all possible enum values above. Rely + // on compiler diagnostics to warn if we haven't. For example, + // GCC's -Wswitch option, enabled by -Wall, will provide a + // warning. } return retval; @@ -1912,8 +1932,18 @@ case mxUINT64_CLASS: return int_to_ov (dv); - default: - panic_impossible (); + case mxCELL_CLASS: + case mxFUNCTION_CLASS: + case mxSTRUCT_CLASS: + case mxUNKNOWN_CLASS: + case mxVOID_CLASS: + error ("invalid conversion from %s%s mxArray to octave_value", (is_complex () ? "complex " : ""), get_class_name ()); + break; + + // We should have handled all possible enum values above. Rely + // on compiler diagnostics to warn if we haven't. For example, + // GCC's -Wswitch option, enabled by -Wall, will provide a + // warning. } return retval; @@ -2210,8 +2240,19 @@ case mxUINT64_CLASS: error ("complex integer types are not supported"); - default: - panic_impossible (); + case mxCELL_CLASS: + case mxCHAR_CLASS: + case mxFUNCTION_CLASS: + case mxSTRUCT_CLASS: + case mxUNKNOWN_CLASS: + case mxVOID_CLASS: + error ("invalid conversion from complex %s mxArray to octave_value", get_class_name ()); + break; + + // We should have handled all possible enum values above. Rely + // on compiler diagnostics to warn if we haven't. For example, + // GCC's -Wswitch option, enabled by -Wall, will provide a + // warning. } return retval; @@ -2374,8 +2415,27 @@ case mxLOGICAL_CLASS: return to_ov (dv); - default: - panic_impossible (); + case mxINT8_CLASS: + case mxUINT8_CLASS: + case mxINT16_CLASS: + case mxUINT16_CLASS: + case mxINT32_CLASS: + case mxUINT32_CLASS: + case mxINT64_CLASS: + case mxUINT64_CLASS: + case mxCELL_CLASS: + case mxCHAR_CLASS: + case mxFUNCTION_CLASS: + case mxSTRUCT_CLASS: + case mxUNKNOWN_CLASS: + case mxVOID_CLASS: + error ("invalid conversion from %s%s sparse mxArray to octave_value", (is_complex () ? "complex " : ""), get_class_name ()); + break; + + // We should have handled all possible enum values above. Rely + // on compiler diagnostics to warn if we haven't. For example, + // GCC's -Wswitch option, enabled by -Wall, will provide a + // warning. } return retval; @@ -2573,8 +2633,28 @@ case mxSINGLE_CLASS: error ("single precision sparse data type not supported"); - default: - panic_impossible (); + case mxLOGICAL_CLASS: + case mxINT8_CLASS: + case mxUINT8_CLASS: + case mxINT16_CLASS: + case mxUINT16_CLASS: + case mxINT32_CLASS: + case mxUINT32_CLASS: + case mxINT64_CLASS: + case mxUINT64_CLASS: + case mxCELL_CLASS: + case mxCHAR_CLASS: + case mxFUNCTION_CLASS: + case mxSTRUCT_CLASS: + case mxUNKNOWN_CLASS: + case mxVOID_CLASS: + error ("invalid conversion from complex %s sparse mxArray to octave_value", get_class_name ()); + break; + + // We should have handled all possible enum values above. Rely + // on compiler diagnostics to warn if we haven't. For example, + // GCC's -Wswitch option, enabled by -Wall, will provide a + // warning. } return retval; diff -r eb8a24370c2b -r 3606817b9994 libinterp/corefcn/mx-type-traits.h --- a/libinterp/corefcn/mx-type-traits.h Sat Apr 06 18:44:45 2024 +0200 +++ b/libinterp/corefcn/mx-type-traits.h Sat Apr 06 14:38:17 2024 -0400 @@ -28,6 +28,8 @@ #include "octave-config.h" +#include + #include "mxtypes.h" #include "oct-inttypes-fwd.h" diff -r eb8a24370c2b -r 3606817b9994 libinterp/corefcn/ordqz.cc --- a/libinterp/corefcn/ordqz.cc Sat Apr 06 18:44:45 2024 +0200 +++ b/libinterp/corefcn/ordqz.cc Sat Apr 06 14:38:17 2024 -0400 @@ -329,9 +329,14 @@ else select(k) = false; break; - default: - // default: case just here to suppress compiler warning. - panic_impossible (); + case NONE: + error ("ordqz: invalid select mode NONE"); + break; + + // We should have handled all possible enum values above. + // Rely on compiler diagnostics to warn if we haven't. + // For example, GCC's -Wswitch option, enabled by -Wall, + // will provide a warning. } } @@ -450,9 +455,14 @@ else select(k) = false; break; - default: - // default: case just here to suppress compiler warning. - panic_impossible(); + case NONE: + error ("ordqz: invalid select mode NONE"); + break; + + // We should have handled all possible enum values above. + // Rely on compiler diagnostics to warn if we haven't. + // For example, GCC's -Wswitch option, enabled by -Wall, + // will provide a warning. } } diff -r eb8a24370c2b -r 3606817b9994 libinterp/octave-value/ov.cc --- a/libinterp/octave-value/ov.cc Sat Apr 06 18:44:45 2024 +0200 +++ b/libinterp/octave-value/ov.cc Sat Apr 06 14:38:17 2024 -0400 @@ -1058,9 +1058,14 @@ m_rep = new octave_bool_matrix (mask, idx); break; - default: - panic_impossible (); + case octave::idx_vector::class_invalid: + error ("unexpected: invalid index in conversion to octave_value - please report this bug"); break; + + // We should have handled all possible enum values above. Rely + // on compiler diagnostics to warn if we haven't. For example, + // GCC's -Wswitch option, enabled by -Wall, will provide a + // warning. } // FIXME: needed? diff -r eb8a24370c2b -r 3606817b9994 libinterp/parse-tree/lex.ll --- a/libinterp/parse-tree/lex.ll Sat Apr 06 18:44:45 2024 +0200 +++ b/libinterp/parse-tree/lex.ll Sat Apr 06 14:38:17 2024 -0400 @@ -2844,12 +2844,14 @@ } break; - default: - panic_impossible (); + // We should have handled all possible enum values above. Rely + // on compiler diagnostics to warn if we haven't. For example, + // GCC's -Wswitch option, enabled by -Wall, will provide a + // warning. } if (! tok) - tok = new token (kw->tok_id, true, m_tok_beg, m_tok_end, get_comment_list ()); + tok = new token (kw->tok_id, true, m_tok_beg, m_tok_end, get_comment_list ()); return tok; }