[PATCH] Handle also switch for -Wdangling-else
Jakub Jelinek
jakub@redhat.com
Wed May 4 19:54:00 GMT 2016
Hi!
This patch let us warn about danling else even if there is a switch
without {}s around the body.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
2016-05-04 Jakub Jelinek <jakub@redhat.com>
* c-parser.c (c_parser_switch_statement): Add IF_P argument,
parse it through to c_parser_c99_block_statement.
(c_parser_statement_after_labels): Adjust c_parser_switch_statement
caller.
* parser.c (cp_parser_selection_statement): For RID_SWITCH,
pass if_p instead of NULL to cp_parser_implicitly_scoped_statement.
* c-c++-common/Wdangling-else-4.c: New test.
--- gcc/c/c-parser.c.jj 2016-05-03 00:12:10.000000000 +0200
+++ gcc/c/c-parser.c 2016-05-04 18:09:27.384953312 +0200
@@ -1305,7 +1305,7 @@ static void c_parser_statement (c_parser
static void c_parser_statement_after_labels (c_parser *, bool *,
vec<tree> * = NULL);
static void c_parser_if_statement (c_parser *, bool *, vec<tree> *);
-static void c_parser_switch_statement (c_parser *);
+static void c_parser_switch_statement (c_parser *, bool *);
static void c_parser_while_statement (c_parser *, bool, bool *);
static void c_parser_do_statement (c_parser *, bool);
static void c_parser_for_statement (c_parser *, bool, bool *);
@@ -5138,7 +5138,7 @@ c_parser_statement_after_labels (c_parse
c_parser_if_statement (parser, if_p, chain);
break;
case RID_SWITCH:
- c_parser_switch_statement (parser);
+ c_parser_switch_statement (parser, if_p);
break;
case RID_WHILE:
c_parser_while_statement (parser, false, if_p);
@@ -5570,7 +5570,7 @@ c_parser_if_statement (c_parser *parser,
*/
static void
-c_parser_switch_statement (c_parser *parser)
+c_parser_switch_statement (c_parser *parser, bool *if_p)
{
struct c_expr ce;
tree block, expr, body, save_break;
@@ -5605,7 +5605,7 @@ c_parser_switch_statement (c_parser *par
c_start_case (switch_loc, switch_cond_loc, expr, explicit_cast_p);
save_break = c_break_label;
c_break_label = NULL_TREE;
- body = c_parser_c99_block_statement (parser, NULL/*if??*/);
+ body = c_parser_c99_block_statement (parser, if_p);
c_finish_case (body, ce.original_type);
if (c_break_label)
{
--- gcc/cp/parser.c.jj 2016-05-03 00:12:11.000000000 +0200
+++ gcc/cp/parser.c 2016-05-04 18:15:30.614109144 +0200
@@ -10978,7 +10978,7 @@ cp_parser_selection_statement (cp_parser
in_statement = parser->in_statement;
parser->in_switch_statement_p = true;
parser->in_statement |= IN_SWITCH_STMT;
- cp_parser_implicitly_scoped_statement (parser, NULL,
+ cp_parser_implicitly_scoped_statement (parser, if_p,
guard_tinfo);
parser->in_switch_statement_p = in_switch_statement_p;
parser->in_statement = in_statement;
--- gcc/testsuite/c-c++-common/Wdangling-else-4.c.jj 2016-05-04 18:40:17.628299460 +0200
+++ gcc/testsuite/c-c++-common/Wdangling-else-4.c 2016-05-04 18:36:19.000000000 +0200
@@ -0,0 +1,31 @@
+/* { dg-do compile } */
+/* { dg-options "-Wdangling-else" } */
+
+void bar (int);
+
+void
+foo (int a, int b, int c)
+{
+ if (a) /* { dg-warning "suggest explicit braces to avoid ambiguous .else." } */
+ switch (b)
+ case 0:
+ if (c)
+ bar (1);
+ else
+ bar (2);
+}
+
+void
+baz (int a, int b, int c)
+{
+ if (a)
+ switch (b)
+ {
+ case 0:
+ if (c)
+ bar (1);
+ }
+ else
+ bar (2);
+}
+
Jakub
More information about the Gcc-patches
mailing list