http://gcc.gnu.org/bugzilla/show_bug.cgi?id=7652

--- Comment #23 from Manuel López-Ibáñez <manu at gcc dot gnu.org> ---
(In reply to Michael Chapman from comment #21)
> Created attachment 32716 [details]
> Proposed patch
> 
> Patch to enable warnings (-Wswitch-fallthrough) when a switch case falls
> through. Enabled by -Wall.

Thanks! Patches need to be submitted to gcc-patc...@gcc.gnu.org with a
Changelog after bootstrapping and regression testing. The patch is missing a
testcase for the regression testsuite showing in which cases it should warn and
in which cases it should not. First of all, you need to have a copyright
assignment in place with the FSF. This is slightly annoying to do the first
time, but you only have to do it once for all GNU projects. See:
http://gcc.gnu.org/contribute.html

About the patch:

+int
+c_stmt_ends_with_goto (tree t)

This should return 'bool'

+{
+  if (TREE_CODE (t) == GOTO_EXPR)
+    return TRUE;
+  if (TREE_CODE (t) == BIND_EXPR)
+    return c_stmt_ends_with_goto (tsi_stmt (tsi_last (BIND_EXPR_BODY (t))));
+  return FALSE;
+}

You can use 'true' and 'false'

+
+/* Handle -Wswitch-fallthrough */
+void 
+c_do_switch_fallthru_warnings (tree body)
+{
+  tree_stmt_iterator i;
+  tree previous_stmt = NULL;
+  tree previous_label = NULL;
+  tree stmts = BIND_EXPR_BODY (body);

I think it would be worthwhile to add:

if (!warn_switch_fallthrough)
   return;

to avoid going through the loop if we are not going to warn anyway.


+Wswitch-fallthrough
+C ObjC C++ ObjC++ Var(warn_switch_fallthrough) Warning LangEnabledBy(C ObjC
C++ ObjC++,Wall)
+Warn about switch cases which fall through to the next case
+

This says that the warning is available in C++, but I don't see any code in
your patch that calls the new function from the C++ FE.

It would be nice to have the same warning in C++. This will allow testing how
noisy it is in GCC itself, for instance. But you (or someone else) could do
that as a follow-up.

Reply via email to