On Fri, Nov 24, 2017 at 10:59:53PM +0100, Jakub Jelinek wrote: > The testcase below has a useless break; that causes a bogus -Wreturn-type > warning. The C++ FE already has code to avoid adding a BREAK_STMT > after a return or similar sequence that is known not to return. > The following patch extends block_may_fallthrough to also return false > for SWITCH_STMTs that can't fall through. > > Those are ones with non-empty body where the whole body can't fallthrough, > additionally they need to have a default: case label (or cover the whole > range of values, but that is not what this patch can compute, that would > be too big duplication of the gimplification processing) and no BREAK_STMT. > > For the default: case label we need to look in all SWITCH_BODY children > except for nested SWITCH_STMTs, for BREAK_STMTs also not in > {FOR,DO,WHILE}_BODY. > > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
Actually, thinking about it some more, maybe it would be more efficient to gather this information during construction of the SWITCH_STMT in some new flag on the tree, so cxx_block_may_fallthru would just: case SWITCH_STMT: if (SWITCH_STMT_CANT_FALLTHRU_P (stmt) && SWITCH_STMT_BODY (stmt) && !block_may_fallthru (SWITCH_STMT_BODY (stmt))) return false; return true; and /* Set if the body of a switch stmt contains a default: case label and does not contain any break; stmts, thus if SWITCH_STMT_BODY is not empty and doesn't fallthru, then the whole switch stmt can't. */ #define SWITCH_STMT_CANT_FALLTHRU_P(NODE) \ TREE_LANG_FLAG_0 (SWITCH_STMT_CHECK (NODE)) Seems the C++ FE already has switch_stack, so we could just add there a has_default_p, has_break_stmt_p and inside_loop_p flags and both inside of templates and outside in finish_case_label, in finish_break_stmt if actually adding BREAK_STMT and when entering a body of a FOR/DO/WHILE loop tweak those flags. Seems switch_stack is also maintained during pt.c, but we should compute it both during parsing and during pt.c (start with the bit clear on a new SWITCH_STMT). Thoughts on this? Guess for the C FE we should similarly handle SWITCH_EXPR, though a break; in there is a goto outside of the switch, so all we need to note is a default: case label. Jakub