Commit 99cd9857 by Marek Polacek Committed by Marek Polacek

re PR c/70436 (-Wparentheses missing ambiguous else warning)

	PR c/70436
	* c-parser.c (c_parser_statement_after_labels): Add IF_P argument and
	adjust callers.
	(c_parser_statement): Likewise.
	(c_parser_c99_block_statement): Likewise.
	(c_parser_while_statement): Likewise.
	(c_parser_for_statement): Likewise.
	(c_parser_if_body): Don't set IF_P here.
	(c_parser_if_statement): Add IF_P argument.  Set IF_P here.  Warn
	about dangling else here.
	* c-tree.h (c_finish_if_stmt): Adjust declaration.
	* c-typeck.c (c_finish_if_stmt): Remove NESTED_IF parameter.  Don't
	warn about dangling else here.

	* testsuite/gcc.dg/Wparentheses-12.c: New test.
	* testsuite/gcc.dg/Wparentheses-13.c: New test.

From-SVN: r234949
parent 5267cfcc
2016-04-13 Marek Polacek <polacek@redhat.com>
PR c/70436
* c-parser.c (c_parser_statement_after_labels): Add IF_P argument and
adjust callers.
(c_parser_statement): Likewise.
(c_parser_c99_block_statement): Likewise.
(c_parser_while_statement): Likewise.
(c_parser_for_statement): Likewise.
(c_parser_if_body): Don't set IF_P here.
(c_parser_if_statement): Add IF_P argument. Set IF_P here. Warn
about dangling else here.
* c-tree.h (c_finish_if_stmt): Adjust declaration.
* c-typeck.c (c_finish_if_stmt): Remove NESTED_IF parameter. Don't
warn about dangling else here.
2016-04-04 Marek Polacek <polacek@redhat.com>
PR c/70307
......
......@@ -641,7 +641,7 @@ extern tree build_asm_stmt (tree, tree);
extern int c_types_compatible_p (tree, tree);
extern tree c_begin_compound_stmt (bool);
extern tree c_end_compound_stmt (location_t, tree, bool);
extern void c_finish_if_stmt (location_t, tree, tree, tree, bool);
extern void c_finish_if_stmt (location_t, tree, tree, tree);
extern void c_finish_loop (location_t, tree, tree, tree, tree, tree, bool);
extern tree c_begin_stmt_expr (void);
extern tree c_finish_stmt_expr (location_t, tree);
......
......@@ -9974,12 +9974,11 @@ c_finish_case (tree body, tree type)
/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
may be null. NESTED_IF is true if THEN_BLOCK contains another IF
statement, and was not surrounded with parenthesis. */
may be null. */
void
c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
tree else_block, bool nested_if)
tree else_block)
{
tree stmt;
......@@ -10011,39 +10010,6 @@ c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
return;
}
}
/* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
if (warn_parentheses && nested_if && else_block == NULL)
{
tree inner_if = then_block;
/* We know from the grammar productions that there is an IF nested
within THEN_BLOCK. Due to labels and c99 conditional declarations,
it might not be exactly THEN_BLOCK, but should be the last
non-container statement within. */
while (1)
switch (TREE_CODE (inner_if))
{
case COND_EXPR:
goto found;
case BIND_EXPR:
inner_if = BIND_EXPR_BODY (inner_if);
break;
case STATEMENT_LIST:
inner_if = expr_last (then_block);
break;
case TRY_FINALLY_EXPR:
case TRY_CATCH_EXPR:
inner_if = TREE_OPERAND (inner_if, 0);
break;
default:
gcc_unreachable ();
}
found:
if (COND_EXPR_ELSE (inner_if))
warning_at (if_locus, OPT_Wparentheses,
"suggest explicit braces to avoid ambiguous %<else%>");
}
stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
SET_EXPR_LOCATION (stmt, if_locus);
......
2016-04-13 Marek Polacek <polacek@redhat.com>
PR c/70436
* testsuite/gcc.dg/Wparentheses-12.c: New test.
* testsuite/gcc.dg/Wparentheses-13.c: New test.
2016-04-13 Ilya Enkovich <ilya.enkovich@intel.com>
* gcc.target/i386/avx512bw-kunpckdq-2.c: New test.
......
/* PR c/70436 */
/* { dg-options "-Wparentheses" } */
int a, b, c;
void bar (void);
void baz (void);
void
foo (void)
{
int i, j;
if (a) /* { dg-warning "ambiguous" } */
for (;;)
if (b)
bar ();
else
baz ();
if (a) /* { dg-warning "ambiguous" } */
while (1)
if (b)
bar ();
else
baz ();
if (a) /* { dg-warning "ambiguous" } */
while (1)
for (;;)
if (b)
bar ();
else
baz ();
if (a) /* { dg-warning "ambiguous" } */
while (1)
while (1)
if (b)
bar ();
else
baz ();
if (a) /* { dg-warning "ambiguous" } */
for (i = 0; i < 10; i++)
for (j = 0; j < 10; j++)
if (b)
bar ();
else
baz ();
if (a)
for (i = 0; i < 10; i++)
if (b) /* { dg-warning "ambiguous" } */
for (j = 0; j < 10; j++)
if (c)
bar ();
else
baz ();
if (a) /* { dg-warning "ambiguous" } */
for (i = 0; i < 10; i++)
if (b)
for (j = 0; j < 10; j++)
if (c)
bar ();
else
baz ();
else
bar ();
if (a) /* { dg-warning "ambiguous" } */
for (;;)
if (b)
while (1)
if (a)
bar ();
else
baz ();
else
bar ();
if (a) /* { dg-warning "ambiguous" } */
for (;;)
if (b)
while (1)
{
if (a) { bar (); } else { baz (); }
}
else
bar ();
if (a)
for (;;)
if (b)
bar ();
else
baz ();
else bar ();
if (a)
while (1)
if (b)
bar ();
else
baz ();
else bar ();
if (a)
for (;;)
{
if (b)
bar ();
else
baz ();
}
if (a)
{
for (;;)
if (b)
bar ();
}
else baz ();
if (a)
do
if (b) bar (); else baz ();
while (b);
if (a)
do
if (b) bar ();
while (b);
else baz ();
}
/* PR c/70436 */
/* { dg-options "-Wparentheses" } */
int a, b, c;
void bar (int);
void
foo (void)
{
if (a) /* { dg-warning "ambiguous" } */
if (b)
{
if (c)
bar (0);
}
else
bar (1);
if (a > 0)
if (a > 1)
if (a > 2)
if (a > 3)
if (a > 4)
if (a > 5) /* { dg-warning "ambiguous" } */
if (a > 6)
while (1)
bar (0);
else
bar (1);
if (a) /* { dg-warning "ambiguous" } */
if (b)
switch (c);
else
bar (1);
switch (a)
{
default:
if (b) /* { dg-warning "ambiguous" } */
if (c)
for (;;)
bar (0);
else
bar (1);
}
if (a) /* { dg-warning "ambiguous" } */
if (a)
{
bar (2);
}
else
bar (3);
if (a)
do if (b) bar (4); while (1);
else bar (5);
do
{
if (a)
if (b) /* { dg-warning "ambiguous" } */
if (c) for (;;) bar (6);
else bar (7);
} while (0);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment